The rtypwidth() function

The rtypwidth() function returns the minimum number of characters that a character data type needs to avoid truncation when you convert a value with an SQL data type to a character data type.

Syntax

mint rtypwidth(sqltype, sqllen)
   mint sqltype;
   mint sqllen;
sqltype
The integer code of the SQL data type. For more information, see Data type constants.
sqllen
The number of bytes in the data file for the specified SQL data type.

Usage

The rtypwidth() function is provided for use with the sqlda structure that a DESCRIBE statement initializes. The sqltype and sqllen components correspond to the components of the same name in each sqlda.sqlvar structure.

Return codes

0
The sqltype is not a valid SQL data type.
>0
The return value is the minimum number of characters that the sqltype data type requires.

Example

This sample program is in the rtypwidth.ec file in the demo directory.
/*
   * rtypwidth.ec *

   This program displays the name of each column in the 'orders' table and
   the number of characters required to store the column when the  
   data type is converted to characters.
*/

#include <stdio.h>

#define WARNNOTIFY      1
#define NOWARNNOTIFY    0

main(argc, argv)
int argc;
char *argv[];
{
    mint i, numchars;

    int4 exp_chk();
    char db_stmnt[50];
    struct sqlda *sql_desc;
    struct sqlvar_struct *col;

    EXEC SQL BEGIN DECLARE SECTION;
   char db_name[20];
    EXEC SQL END DECLARE SECTION;

    printf("RTYPWIDTH Sample ESQL Program running.\n\n");

    if (argc > 2)            /* correct no. of args? */
      {
      printf("\nUsage: %s [database]\nIncorrect no. of argument(s)\n",
      argv[0]);
      exit(1);
      }
   strcpy(db_name, "stores7");
    if (argc == 2)
      strcpy(db_name, argv[1]);

   EXEC SQL connect to :db_name;
   sprintf(db_stmnt, "CONNECT TO %s", argv[1]);
   exp_chk(db_stmnt, NOWARNNOTIFY);

   printf("Connected to %s\n", db_name);

    EXEC SQL prepare query_1 from 'select * from orders';  /* prepare select */
    if(exp_chk("Prepare", WARNNOTIFY) == 1)
      exit(1);
    EXEC SQL describe query_1 into sql_desc;            /* setup sqlda */
    if(exp_chk("Describe", WARNNOTIFY) == 1)
      exit(1);
    printf("\n\tColumn Name    \t# chars\n");

/*
     * For each column in orders print the column name and the minimum
     * number of characters required to convert the SQL type to a character
     * data type
     */
    for (i = 0, col = sql_desc->sqlvar; i < sql_desc->sqld; i++, col++)
      {
      numchars = rtypwidth(col->sqltype, col->sqllen);
      printf("\t%-15s\t%d\n", col->sqlname, numchars);
      }

    printf("\nRTYPWIDTH Sample Program over.\n\n");
}

/*
 *  The exp_chk() file contains the exception handling functions to
 *  check the SQLSTATE status variable to see if an error has occurred
 *  following an SQL statement. If a warning or an error has
 *  occurred, exp_chk() executes the GET DIAGNOSTICS statement and
 *  prints the detail for each exception that is returned.
 */
EXEC SQL include exp_chk.ec

Output

RTYPWIDTH Sample ESQL Program running.

Connected to stores7

   Column Name                # chars
   order_num                  11
   order_date                 10
   customer_num               11
   ship_instruct              40
   backlog                    1
   po_num                     10
   ship_date                  10
   ship_weight                10
   ship_charge                9
   paid_date                  10

RTYPWIDTH Sample Program over.