- Categories:
TO_ CHAR , TO_ VARCHAR¶
Converts the input expression to a string. For NULL input, the output is NULL.
These functions are synonymous.
Syntax¶
Arguments¶
Required:
exprAn expression of any data type.
numeric_exprA numeric expression.
date_or_time_exprAn expression of type DATE, TIME, or TIMESTAMP.
binary_exprAn expression of type BINARY or VARBINARY.
Optional:
formatThe format of the output string:
-
For
numeric_expr, specifies the SQL format model used to interpret the numeric expression. For more information, see SQL format models. -
For
date_or_time_expr, specifies the expected format to parse or produce a string. For more information, see Date and time formats in conversion functions.The default is the current value of the following session parameters:
- DATE_OUTPUT_FORMAT (for DATE inputs)
- TIME_OUTPUT_FORMAT (for TIME inputs)
- TIMESTAMP_OUTPUT_FORMAT (for TIMESTAMP inputs)
-
For
binary_expr, specifies the format in which to produce the string (e.g. ‘HEX’, ‘BASE64’ or ‘UTF-8’).For more information, see Overview of supported binary formats.
-
Returns¶
This function returns a value of VARCHAR data type or NULL.
Usage notes¶
- For VARIANT, ARRAY, or OBJECT inputs, the output is the string containing
a JSON document or JSON elementary value (unless VARIANT or OBJECT
contains an XML tag, in which case the output is a string containing
an XML document):
- A string stored in VARIANT is preserved as is (i.e. it is not converted to a JSON string).
- A JSON null value is converted to a string containing the word “null”.
Examples¶
The following examples convert numbers, timestamps, and dates to strings.
Examples that convert numbers¶
Convert numeric values to strings in the specified formats:
The output illustrates how the values are converted to strings based on the specified formats:
-
The
>and<symbols are string literals that are included in the output. They make it easier to see where spaces are inserted. -
The
D2_1column shows the values with a$printed before the digits.- For the
3987value, there are more digits in the integer part of the number than there are digit positions in the format, so all digits are printed as#to indicate overflow. - For the
0.10,0.01, and1.111values, there are more digits in the fractional part of the number than there are digit positions in the format, so the fractional values are truncated.
- For the
-
The
D4_1column shows that zero values are represented as spaces in the integer parts of the numbers.- For the
0,0.10, and0.01values, a space replaces the zero before the separator. - For the
0.10,0.01, and1.111values, there are more digits in the fractional part of the number than there are digit positions in the format, so the fractional values are truncated.
- For the
-
The
TMEcolumn shows the values in scientific notation. -
The
TM9column shows the values as integers or decimal fractions, based on the value of the number. -
The
X4column shows the values as hexadecimal digits without the fractional parts. -
The
SX4column shows the values as hexadecimal digits of the absolute value of the numbers and includes the numeric sign (+or-).
This example converts a logarithmic value to a string:
Examples that convert timestamps and dates¶
Convert a TIMESTAMP value to a string in the specified format:
Convert a DATE value to a string in the default format:
Convert a DATE value to a string in the specified format: