to_string()

Scalar. Converts a given value to a string.

Syntax

to_string ( value [, format] [, timezone] )

Parameters

value

A value of any datatype.

format

(Optional) A format string. Only valid if the value is a date/time or numeric type.

timezone

(Optional) A time zone. Only valid if the value is a date/time type. If none is specified, the UTC time zone is used.

Usage

Converts a given value to a string. The function can take any datatype as its argument, and the function returns a string. Note that the function can take a string as its argument, but it will return the same string. This function converts values as follows:
  • For integers or longs, the user can include an optional format string to specify the format for the output string. The format string follows the ISO standard for fprintf. The default for integer expressions is '%d', while the default for long expressions is '%lld'.
  • For a date/time type, the user can include to specify the format of the output string. The string must be a valid timestamp format code.
  • The optional time zone argument can only be used with a date/time type. This string must be a valid time zone string. If no time zone is specified, UTC will be used. See "Time Zones" and "List of Time Zones" for more information.
  • The function works the same as xmlserialize() when converting an XML value to a string
  • For binary values, the returned string can contain unprintable characters because the function does a simple cast from binary to string rather than performing a conversion. To convert to a hex string representation of the binary value, use the hex_string() function.
For a float value, the user can include an optional format string that specifies the format for the output of the floating point number as a string. The format string can include the following characters:

. or D

Returns a decimal point in the specified position. Only one decimal point can be specified, or the output will contain number signs instead of the values.

9

Replaced in the output by a single digit of the value. The value is returned with as many characters as there are 9s in the format string.

If the value is positive, a leading space is included to the left of the value. If the value is negative, a leading minus sign is included to the left of the value.

Excess 9s to the left of the decimal point are replaced with spaces, while excess 9s to the right of the decimal point are replaced with zeros. Insufficient 9s to the left of the decimal point returns number signs instead of the value, while insufficient 9s to the right of the decimal point result in rounding.

0

To the left of the decimal point, replaced in the output by a single digit of the value or a zero, if the value does not have a digit in the position of the zero. To the right of the decimal point, treated as a 9.

If the value is positive, a leading space is included to the left of the value. If the value is negative, a leading minus sign is included to the left of the value.

EEEE

Returns the value in scientific notation. The output for this format always includes a single digit before the decimal. Combine with a decimal point and 9s to specify precision. 9s to the left of the decimal point are ignored.

Must be placed at the end of the format string.

S

Returns a leading or trailing minus sign (-) or plus sign (+), depending on whether the value is positive or negative. Can only be placed at the beginning or end of the format string.

Eliminates the usual single leading space, but not leading spaces as the result of excess 9s, zeros, or commas.

$

Returns a leading dollar sign in front of the value. Can be placed anywhere in the format string.

.

Returns a comma in the specified position. If there are no digits to the left of the comma, the comma is replaced with a space.

The user can specify multiple commas, but cannot specify a comma as the first character in the format, or to the right of the decimal point.

FM

Strips spaces from the output.

Examples

to_string (45642) returns '45642'.

to_string (1234.567,'999') returns '####'.

to_string (1234.567,'9999D999') returns '1234.567'.

to_string (1234.567,'.99999999EEEE') returns '1.23456700E+03'.