SQL - Scalar functions - VARCHAR_FORMAT
The VARCHAR_FORMAT function returns a character representation of an input expression.
The schema is SYSIBM.
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 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.
The code page of the result is the code page of the section.
If the input expression returns:
For a list of valid formats for string representations of datetime values, see String representations of datetime values.
The default format string is based on the value of the special register CURRENT LOCALE LC_TIME.
A valid format string must contain a combination of the format elements listed in Table 1 (SQLSTATE 22007). Two format elements can optionally be separated by one or more of the following separator characters:
The format elements in Table 1 are not case sensitive, with the following exceptions:
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", not "D followed by DY followed by YYY".
The specified locale name is not case sensitive and must be a valid locale (SQLSTATE 42815). For information about valid locales and their naming, see Locale names for SQL and XQuery. The default is the value of the CURRENT LOCALE LC_TIME special register.
The result is a representation of the input timestamp expression in the format specified by the format string. The format string is interpreted as a series of format elements that can be separated by one or more separator characters. A string of characters in the format string is interpreted as the longest matching format element in Table 1. If two format elements that contain 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 255. If the string units of the environment or format-string is CODEUNITS32, the string units of the result is CODEUNITS32. Otherwise, the string units of the result is OCTETS. 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.
If a format string is not specified, the input decimal floating-point expression is formatted in the form of an SQL decimal floating-point constant. If the input decimal floating-point expression is negative, the first character of the result is a minus sign; otherwise, the first character is a digit. If the input decimal floating-point expression is zero, the result is 0.
The format elements are case sensitive.
The result is a varying-length character string representation of the input decimal floating-point expression. The length attribute is 255. If the string units of the environment or the format string is CODEUNITS32, the string units of the result is CODEUNITS32; otherwise, the string units of the result is OCTETS. 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 the input decimal floating-point expression. If the resulting string exceeds the length attribute of the result, the result is truncated.
If the value of the input decimal floating-point expression is the special value:
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 the format string does not include any of the format elements MI, S, or PR, and if the value of the input expression is negative, then a minus sign (-) is included in the result; otherwise, a blank is included in the result.
If the number of digits to the left of the decimal point in the input expression is greater than the number of digits to the left of the decimal point in the format string, the result is a string of one or more number sign (#) characters. If the number of digits to the right of the decimal point in the input expression is greater than the number of digits to the right of the decimal point in the format string, the result is rounded to the number of digits to the right of the decimal point in the format string. The DECFLOAT rounding mode will not be used. The rounding behavior of VARCHAR_FORMAT corresponds to a value of ROUND_HALF_UP.
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%'
TABLE_NAME CREATION_TIME -------------------- ------------------- SYSUSERAUTH 2000-05-19 08:18:56 SYSUSEROPTIONS 2000-05-19 08:18:56
Function invocation Result ------------------- ------ VARCHAR_FORMAT(TMSTMP,'YYYYMMDDHHMISSFF3') 20070309020738123 VARCHAR_FORMAT(TMSTMP,'YYYYMMDDHH24MISS') 20070309140738 VARCHAR_FORMAT(TMSTMP,'YYYYMMDDHHMI') 200703090207
VARCHAR_FORMAT(TMSTMP,'HH12:MI:SS.MS') 14:07:38.123 VARCHAR_FORMAT(TMSTMP,'H12:MI:SS.US') 14:07:38.123456
VARCHAR_FORMAT(TMSTMP,'DD/MM/YY') 09/03/07 VARCHAR_FORMAT(TMSTMP,'MM-DD-YYYY') 03-09-2007 VARCHAR_FORMAT(TMSTMP,'J') 2454169 VARCHAR_FORMAT(TMSTMP,'Q') 1 VARCHAR_FORMAT(TMSTMP,'W') 2 VARCHAR_FORMAT(TMSTMP,'IW') 10 VARCHAR_FORMAT(TMSTMP,'WW') 10 VARCHAR_FORMAT(TMSTMP,'Month','en_US') March VARCHAR_FORMAT(TMSTMP,'MONTH','en_US') MARCH VARCHAR_FORMAT(TMSTMP,'MON','en_US') MAR VARCHAR_FORMAT(TMSTMP,'Day','en_US') Friday VARCHAR_FORMAT(TMSTMP,'DAY','en_US') FRIDAY VARCHAR_FORMAT(TMSTMP,'Dy','en_US') Fri VARCHAR_FORMAT(TMSTMP,'Month','de_DE') März VARCHAR_FORMAT(TMSTMP,'MONTH','de_DE') MÄRZ VARCHAR_FORMAT(TMSTMP,'MON','de_DE') MRZ VARCHAR_FORMAT(TMSTMP,'Day','de_DE') Freitag VARCHAR_FORMAT(TMSTMP,'DAY','de_DE') FREITAG VARCHAR_FORMAT(TMSTMP,'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'
VARCHAR_FORMAT(POSNUM,'99,99,99') '12,34,56' VARCHAR_FORMAT(NEGNUM,'99,99,99') '-12,34,56' VARCHAR_FORMAT(POSNUM,'PL9999.99') '+1234.56' VARCHAR_FORMAT(NEGNUM,'PL9999.99') '-1234.56' VARCHAR_FORMAT(POSNUM,'9999PL') '1234+' VARCHAR_FORMAT(NEGNUM,'9999PL') '1234' VARCHAR_FORMAT(POSNUM,'9999.9') '1234.6' VARCHAR_FORMAT(NEGNUM,'9999.9') '-1234.6' VARCHAR_FORMAT(POSNUM,'9999') '1235' VARCHAR_FORMAT(NEGNUM,'9999') '-1235' VARCHAR_FORMAT(POSNUM,'99.99') '#####' VARCHAR_FORMAT(NEGNUM,'99.99') '#####' VARCHAR_FORMAT(POSNUM,'9999D99', 'en_US') '1234.56' VARCHAR_FORMAT(POSNUM,'9999D99', 'fr_FR') '1234,56' VARCHAR_FORMAT(POSNUM,'9G999D99', 'en_US') '1,234.56' VARCHAR_FORMAT(POSNUM,'9G999D99', 'de_DE') '1.234,56'