Skip to main content

TO_STRING

Introduced or updated: v1.2.745

Converts a value to String data type, or converts a Date value to a specific string format. To customize the format of date and time in Databend, you can utilize specifiers. These specifiers allow you to define the desired format for date and time values. For a comprehensive list of supported specifiers, see Formatting Date and Time.

Syntax​

TO_STRING( '<expr>' )

TO_STRING( '<date>', '<format>' )

Binary input​

Introduced or updated: v1.2.964-nightly
TO_STRING( <binary_expr>, '<format>' )

For Binary input, format is a character set label, hex, or base64:

  • A character set label decodes bytes to a String. For example, gbk decodes GBK bytes. See CONVERT_FROM for supported labels and decoding errors.
  • hex returns lowercase hexadecimal text. base64 returns Base64 text. These formats do not decode a character set.

The format must be a scalar constant, not a column. A NULL input or scalar format returns NULL. Invalid bytes or an unsupported label raise an error. TRY_TO_STRING returns NULL for conversion failures, but still rejects a column format.

Without a format argument, Binary input uses UTF-8 decoding. Only binary_output_format = 'utf-8-lossy' permits replacement of invalid UTF-8 bytes. This setting does not change explicit character set decoding.

For Base64-wrapped GBK data, decode the Base64 layer first:

SELECT TO_STRING(FROM_BASE64('xOO6ww=='), 'gbk');
-- 你好

Date Format Styles​

Databend supports two date format styles that can be selected using the date_format_style setting:

  • MySQL (default): Uses MySQL-compatible format specifiers like %Y, %m, %d, etc.
  • Oracle: Uses format specifiers like YYYY, MM, DD, etc., which follow a standardized format commonly used in many database systems.

To switch between format styles, use the date_format_style setting:

-- Set Oracle-style date format
SETTINGS (date_format_style = 'Oracle') SELECT to_string('2024-04-05'::DATE, 'YYYY-MM-DD');

-- Set MySQL date format style (default)
SETTINGS (date_format_style = 'MySQL') SELECT to_string('2024-04-05'::DATE, '%Y-%m-%d');

Oracle-Style Format Specifiers​

When date_format_style is set to 'Oracle', the following format specifiers are supported:

Format SpecifierDescriptionExample Output (for '2024-04-05 14:30:45.123456')
YYYY4-digit year2024
YY2-digit year24
MMMMFull month nameApril
MONAbbreviated month nameApr
MMMonth number (01-12)04
DDDay of month (01-31)05
DYAbbreviated day nameFri
HH24Hour of day (00-23)14
HH12Hour of day (01-12)02
AM/PMMeridian indicatorPM
MIMinute (00-59)30
SSSecond (00-59)45
FFFractional seconds123456
UUUUISO week-numbering year2024
TZH:TZMTime zone hour and minute with colon+08:00
TZHTime zone hour+08

Aliases​

Return Type​

String.

Examples​

SELECT
DATE_FORMAT('1.23'),
TO_STRING('1.23'),
TO_TEXT('1.23'),
TO_VARCHAR('1.23'),
JSON_TO_STRING('1.23');

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ date_format('1.23') │ to_string('1.23') │ to_text('1.23') │ to_varchar('1.23') │ json_to_string('1.23') │
├─────────────────────┼───────────────────┼─────────────────┼────────────────────┼────────────────────────┤
│ 1.23 │ 1.23 │ 1.23 │ 1.23 │ 1.23 │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘

SELECT
DATE_FORMAT('["Cooking", "Reading"]' :: JSON),
TO_STRING('["Cooking", "Reading"]' :: JSON),
TO_TEXT('["Cooking", "Reading"]' :: JSON),
TO_VARCHAR('["Cooking", "Reading"]' :: JSON),
JSON_TO_STRING('["Cooking", "Reading"]' :: JSON);

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ date_format('["cooking", "reading"]'::variant) │ to_string('["cooking", "reading"]'::variant) │ to_text('["cooking", "reading"]'::variant) │ to_varchar('["cooking", "reading"]'::variant) │ json_to_string('["cooking", "reading"]'::variant) │
├────────────────────────────────────────────────┼──────────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────────┼───────────────────────────────────────────────────┤
│ ["Cooking","Reading"] │ ["Cooking","Reading"] │ ["Cooking","Reading"] │ ["Cooking","Reading"] │ ["Cooking","Reading"] │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

-- With one argument, the function converts input to a string without validating as a date.
SELECT
DATE_FORMAT('20223-12-25'),
TO_STRING('20223-12-25'),
TO_TEXT('20223-12-25'),
TO_VARCHAR('20223-12-25'),
JSON_TO_STRING('20223-12-25');

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ date_format('20223-12-25') │ to_string('20223-12-25') │ to_text('20223-12-25') │ to_varchar('20223-12-25') │ json_to_string('20223-12-25') │
├────────────────────────────┼──────────────────────────┼────────────────────────┼───────────────────────────┼───────────────────────────────┤
│ 20223-12-25 │ 20223-12-25 │ 20223-12-25 │ 20223-12-25 │ 20223-12-25 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

-- Using MySQL format style (default)
SELECT
DATE_FORMAT('2022-12-25', '%m/%d/%Y'),
TO_STRING('2022-12-25', '%m/%d/%Y'),
TO_TEXT('2022-12-25', '%m/%d/%Y'),
TO_VARCHAR('2022-12-25', '%m/%d/%Y'),
JSON_TO_STRING('2022-12-25', '%m/%d/%Y');

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ date_format('2022-12-25', '%m/%d/%Y') │ to_string('2022-12-25', '%m/%d/%Y') │ to_text('2022-12-25', '%m/%d/%Y') │ to_varchar('2022-12-25', '%m/%d/%Y') │ json_to_string('2022-12-25', '%m/%d/%Y') │
├───────────────────────────────────────┼─────────────────────────────────────┼───────────────────────────────────┼──────────────────────────────────────┼──────────────────────────────────────────┤
│ 12/25/2022 │ 12/25/2022 │ 12/25/2022 │ 12/25/2022 │ 12/25/2022 │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

-- Using Oracle format style (same data as MySQL example above)
SETTINGS (date_format_style = 'Oracle')
SELECT
TO_STRING('2022-12-25', 'MM/DD/YYYY'),
TO_CHAR('2022-12-25', 'MM/DD/YYYY'); -- Using TO_CHAR alias

┌─────────────────────────────────────────────────────────────────┐
│ to_string('2022-12-25', 'MM/DD/YYYY') │ to_char('2022-12-25', 'MM/DD/YYYY') │
├─────────────────────────────────────┼───────────────────────────────────┤
│ 12/25/2022 │ 12/25/2022 │

└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Try Databend Cloud for FREE

Multimodal, object-storage-native warehouse for BI, vectors, search, and geo.

Snowflake-compatible SQL with automatic scaling.

Sign up and get $200 in credits.

Try it today