Character to varchar
Timestamp to varchar:
>>-VARCHAR_FORMAT--(--timestamp-expression--+--------------------------------------+--)->< '-,--format-string--+----------------+-' '-,--locale-name-'
Decimal floating-point to varchar:
>>-VARCHAR_FORMAT--(--decimal-floating-point-expression--+------------------+--)->< '-,--format-string-'
The schema is SYSIBM.
The VARCHAR_FORMAT function returns a character string based on applying the specified format string argument, if provided, to the value of the first argument. If any argument of the VARCHAR_FORMAT function can be null, the result can be null; if any argument is null, the result is the null value.
The expression must be formatted according to a specified character template.
The result is a VARCHAR with a length attribute that matches the length attribute of the argument. The value of the result is the same as the value of character-expression.
If timestamp-expression is a DATE or a valid string representation of a date, it is first converted to a TIMESTAMP(0) value, assuming a time of exactly midnight (00.00.00).
For the valid formats of string representations of datetime values, see "String representations of datetime values" in "Datetime values".
Format element | Description |
---|---|
AM or PM | Meridian indicator (morning or evening) without periods. This format element is dependent on locale-name, if specified; otherwise, it is dependent on the value of the special register CURRENT LOCALE LC_TIME. |
A.M. or P.M. | Meridian indicator (morning or evening) with periods. This format element uses the exact strings ‘A.M.' or ‘P.M.' and is independent of the locale name in effect. |
CC | Century (01-99). If the last two digits of the four-digit year are zero, the result is the first two digits of the year; otherwise, the result is the first two digits of the year plus one. |
DAY, Day, or day | Name of the day in uppercase, titlecase, or lowercase format. The language used is dependent on locale-name, if specified; otherwise, it is dependent on the value of the special register CURRENT LOCALE LC_TIME. |
DY, Dy, or dy | Abbreviated name of the day in uppercase, titlecase, or lowercase format. The language used is dependent on locale-name, if specified; otherwise, it is dependent on the value of the special register CURRENT LOCALE LC_TIME. |
D | Day of the week (1-7). The first day of the week is dependent on locale-name, if specified; otherwise, it is dependent on the value of the special register CURRENT LOCALE LC_TIME. |
DD | Day of month (01-31). |
DDD | Day of year (001-366). |
FF or FFn | Fractional seconds (0-999999999999). The number n is used to specify the number of digits to include in the returned value. Valid values for n are 1-12 with no leading zeros. Specifying FF is equivalent to specifying FF6. If the timestamp precision of timestamp-expression is less than what is specified by the format, zero digits are padded onto the right of the specified digits. |
HH | HH behaves the same as HH12. |
HH12 | Hour of the day (01-12) in 12-hour format. |
HH24 | Hour of the day (00-24) in 24-hour format. |
IW | ISO week of the year (01-53). The week starts on Monday and includes seven days. Week 1 is the first week of the year to contain a Thursday, which is equivalent to the first week of the year to contain January 4. |
I | ISO year (0-9). The last digit of the year based on the ISO week that is returned. |
IY | ISO year (00-99). The last two digits of the year based on the ISO week that is returned. |
IYY | ISO year (000-999). The last three digits of the year based on the ISO week that is returned. |
IYYY | ISO year (0000-9999). The 4-digit year based on the ISO week that is returned. |
J | Julian day (number of days since January 1, 4713 BC). |
MI | Minute (00-59). |
MM | Month (01-12). |
NNNNNN | Microseconds (000000-999999). Same as FF6. |
MONTH, Month, or month | Name of the month in uppercase, titlecase, or lowercase format. The language used is dependent on locale-name, if specified; otherwise, it is dependent on the value of the special register CURRENT LOCALE LC_TIME. |
MON, Mon, or mon | Abbreviated name of the month in uppercase, titlecase, or lowercase format. The language used is dependent on locale-name, if specified; otherwise, it is dependent on the value of the special register CURRENT LOCALE LC_TIME. |
Q | Quarter (1-4), where the months January through March return 1. |
RR | RR behaves the same as YY. |
RRRR | RRRR behaves the same as YYYY. |
SS | Seconds (00-59). |
SSSSS | Seconds since previous midnight (00000-86400). |
W | Week of the month (1-5), where week 1 starts on the first day of the month and ends on the seventh day. |
WW | Week of the year (01-53), where week 1 starts on January 1 and ends on January 7. |
Y | Last digit of the year (0-9). |
YY | Last two digits of the year (00-99). |
YYY | Last three digits of the year (000-999). |
YYYY | 4-digit year (0000-9999). |
In cases where format elements are ambiguous, the case insensitive format elements will be considered first. For example, DDYYYY would be interpreted as DD followed by YYYY, rather than D followed by DY, followed by YYY.
If format-string is not specified, the format used is based on the value of the special register CURRENT LOCALE LC_TIME.
The value of locale-name is not case sensitive and must be a valid locale (SQLSTATE 42815). For information on valid locales and their naming, see "Locale names for SQL and XQuery". If locale-name is not specified, the value of the special register CURRENT LOCALE LC_TIME is used.
The result is a representation of timestamp-expression in the format specified by format-string. The format-string is interpreted as a series of format elements that can optionally be separated by one or more separator characters. A string of characters in format-string is interpreted as the longest matching format element in Table 1. If two format elements containing the same characters are not delimited by a separator character, the specification is interpreted, starting from the left, as the longest matching format element in the table, and continues until matches are found for the remainder of the format string. For example, 'YYYYYYDD' is interpreted as the format elements 'YYYY', 'YY', and 'DD'.
The result is a varying-length character string. The length attribute is 254. The format-string determines the actual length of the result. If the resulting string exceeds the length attribute of the result, the result is truncated.
Format element | Description |
---|---|
0 | Each 0 represents a significant digit. Leading zeros in a number are displayed as zeros. |
9 | Each 9 represents a significant digit. Leading zeros in a number are displayed as blanks. |
MI | Suffix: If decimal-floating-point-expression is a negative number, a trailing minus sign (−) is included in the result. If decimal-floating-point-expression is a positive number, a trailing blank is included in the result. |
S | Prefix: If decimal-floating-point-expression is a negative number, a leading minus sign (−) is included in the result. If decimal-floating-point-expression is a positive number, a leading plus sign (+) is included in the result. |
PR | Suffix: If decimal-floating-point-expression is a negative number, a leading less than character (<) and a trailing greater than character (>) are included in the result. If decimal-floating-point-expression is a positive number, a leading space and a trailing space are included in the result. |
$ | Prefix: A leading dollar sign ($) is included in the result. |
, | Specifies that a comma be included in that location in the result. This comma is used as a group separator. |
. | Specifies that a period be included in that location in the result. This period is used as a decimal point. |
If format-string is not specified, decimal-floating-point-expression is formatted in the form of an SQL decimal floating-point constant. If decimal-floating-point-expression is negative, the first character of the result is a minus sign; otherwise, the first character is a digit. If decimal-floating-point-expression is zero, the result is 0.
The result is a varying-length character string representation of decimal-floating-point-expression. The length attribute is 254. The actual length of the result is determined by the format-string, if specified; otherwise, the actual length of the result is the smallest number of characters that can represent the value of decimal-floating-point-expression. If the resulting string exceeds the length attribute of the result, the result will be truncated.
If the value of decimal-floating-point-expression is the special value Infinity, sNaN, or NaN, the strings "INFINITY", "SNAN", and "NAN", respectively, are returned. If the special value is negative, the first character of the result is a minus sign. The decimal floating-point special value sNaN does not result in an exception when converted to a string.
If format-string does not include any of the format elements MI, S, or PR, and the value of decimal-floating-point-expression is negative, then a minus sign (−) will be included in the result; otherwise, a blank will be included in the result.
If the number of digits to the left of the decimal point in decimal-floating-point-expression is greater than the number of digits to the left of the decimal point in format-string, the result is a string of number sign (#) characters. If the number of digits to the right of the decimal point in decimal-floating-point-expression is greater than the number of digits to the right of the decimal point in format-string, the result is decimal-floating-point-expression rounded to the number of digits to the right of the decimal point in format-string. The DECFLOAT rounding mode will not be used. The rounding behavior of VARCHAR_FORMAT corresponds to a value of ROUND_HALF_UP.
The code page of the result is the code page of the section.
SELECT VARCHAR(TABNAME, 20) AS TABLE_NAME, VARCHAR_FORMAT(CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') AS CREATION_TIME FROM SYSCAT.TABLES WHERE TABNAME LIKE 'SYSU%'This example returns the following:
TABLE_NAME CREATION_TIME -------------------- ------------------- SYSUSERAUTH 2000-05-19 08:18:56 SYSUSEROPTIONS 2000-05-19 08:18:56
Function invocation Result ------------------- ------ VARCHAR_FORMAT(TMSTAMP,'YYYYMMDDHHMISSFF3') 20070309020738123 VARCHAR_FORMAT(TMSTAMP,'YYYYMMDDHH24MISS') 20070309140738 VARCHAR_FORMAT(TMSTAMP,'YYYYMMDDHHMI') 200703090207 VARCHAR_FORMAT(TMSTAMP,'DD/MM/YY') 09/03/07 VARCHAR_FORMAT(TMSTAMP,'MM-DD-YYYY') 03-09-2007 VARCHAR_FORMAT(TMSTAMP,'J') 2454169 VARCHAR_FORMAT(TMSTAMP,'Q') 1 VARCHAR_FORMAT(TMSTAMP,'W') 2 VARCHAR_FORMAT(TMSTAMP,'IW') 10 VARCHAR_FORMAT(TMSTAMP,'WW') 10 VARCHAR_FORMAT(TMSTAMP,'Month','en_US') March VARCHAR_FORMAT(TMSTAMP,'MONTH','en_US') MARCH VARCHAR_FORMAT(TMSTAMP,'MON','en_US') MAR VARCHAR_FORMAT(TMSTAMP,'Day','en_US') Friday VARCHAR_FORMAT(TMSTAMP,'DAY','en_US') FRIDAY VARCHAR_FORMAT(TMSTAMP,'Dy','en_US') Fri VARCHAR_FORMAT(TMSTAMP,'Month','de_DE') März VARCHAR_FORMAT(TMSTAMP,'MONTH','de_DE') MÄRZ VARCHAR_FORMAT(TMSTAMP,'MON','de_DE') MRZ VARCHAR_FORMAT(TMSTAMP,'Day','de_DE') Freitag VARCHAR_FORMAT(TMSTAMP,'DAY','de_DE') FREITAG VARCHAR_FORMAT(TMSTAMP,'Dy','de_DE') Fr
Function invocation Result ------------------- ------ VARCHAR_FORMAT(DTE,'YYYYMMDD') 20070309 VARCHAR_FORMAT(DTE,'YYYYMMDDHH24MISS') 20070309000000
Function invocation Result ------------------- ------ VARCHAR_FORMAT(POSNUM) '1234.56' VARCHAR_FORMAT(NEGNUM) '-1234.56' VARCHAR_FORMAT(POSNUM,'9999.99') '1234.56' VARCHAR_FORMAT(NEGNUM,'9999.99') '1234.56' VARCHAR_FORMAT(POSNUM,'99999.99') ' 1234.56' VARCHAR_FORMAT(NEGNUM,'99999.99') ' 1234.56' VARCHAR_FORMAT(POSNUM,'00000.00') '01234.56' VARCHAR_FORMAT(NEGNUM,'00000.00') '01234.56' VARCHAR_FORMAT(POSNUM,'9999.99MI') '1234.56 ' VARCHAR_FORMAT(NEGNUM,'9999.99MI') '1234.56-' VARCHAR_FORMAT(POSNUM,'S9999.99') '+1234.56' VARCHAR_FORMAT(NEGNUM,'S9999.99') '-1234.56' VARCHAR_FORMAT(POSNUM,'9999.99PR') ' 1234.56 ' VARCHAR_FORMAT(NEGNUM,'9999.99PR') '<1234.56>' VARCHAR_FORMAT(POSNUM,'S$9,999.99') '+$1,234.56' VARCHAR_FORMAT(NEGNUM,'S$9,999.99') '-$1,234.56'