TO_STRING()

Scalar. Converts a given value to a String.

Syntax

TO_STRING( value [, format] [, timezone] )
Parameters

value

The value to convert.

format

A format string. Only valid if value is an Integer, Long, Float, or Timestamp.

timezone

A time zone. Only valid if value is a Timestamp. If omitted, assumes the local time zone.

Data Types

Return

value

format

timezone

String

Integer

String

N/A

Long

Float

Timestamp

String

Interval

N/A

N/A

String

BLOB

Boolean

XML

Usage

This function coverts values as follows:

Character

Description

. 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 value.

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 zeroes. 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. Use FM to strip leading spaces.

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. Use FM to strip leading spaces.

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, zeroes, 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. You 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 leading spaces from the output.

The following table shows several examples of format strings and the resulting output. Note that a space in the output is represented by a small circle (·):

Example

Output

TO_STRING(1234,'9999')

"·1234"

TO_STRING(1234.567,'9999')

"·1235"

TO_STRING(1234.567,'999')

"####"

TO_STRING(1234.567,'9999D999')

"·1234.567"

TO_STRING(1234.567,'9999D9')

"·1234.6"

TO_STRING(1234.567,'9999.9999')

"·1234.5670"

TO_STRING(1234.567,'999999.9999')

"···1234.5670"

TO_STRING(1234.567,'009999.999')

"·001234.567"

TO_STRING(1234.567,'00000.999')

"·01234.567"

TO_STRING(1234.567,'9,999.999')

"·1,234.567"

TO_STRING(1234.567,'9,9,999.999')

"···1,234.567"

TO_STRING(1234.567,'FM9,9,999.999')

"1,234.567"

TO_STRING(1234.567,'$9,999.999')

"·$1,234.567"

TO_STRING(1234.567,'9,999.$999')

"·$1,234.567"

TO_STRING(1234.567,'EEEE')

"1E+03"

TO_STRING(1234.567,'.999EEEE')

"1.235E+03"

TO_STRING(1234.567,'999.999EEEE')

"1.235E+03"

TO_STRING(1234.567,'.99999999EEEE')

"1.23456700E+03"

TO_STRING(1234.567,'S9,999.99')

"+1,234.57"

TO_STRING(1234.567,'9,999.99S')

"1,234.57+"

TO_STRING(1234.567,'9,9,999.99S')

"··1,234.57+"

TO_STRING(1234.567,'FM9,9,999.99S')

"1,234.57+"

Examples

The following example converts a numeric value to a string:

INSERT INTO OutStream
SELECT TO_STRING(Trades.Price)
FROM Trades;

The following example converts a TIMESTAMP to a STRING with a format specifying precision:

INSERT INTO OutStream
SELECT TO_STRING(Trades.TradeTime, 'YYYY-MM-DD HH:MI')
FROM Trades;