TO_STRING
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
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,
gbkdecodes GBK bytes. See CONVERT_FROM for supported labels and decoding errors. hexreturns lowercase hexadecimal text.base64returns 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 Specifier | Description | Example Output (for '2024-04-05 14:30:45.123456') |
|---|---|---|
| YYYY | 4-digit year | 2024 |
| YY | 2-digit year | 24 |
| MMMM | Full month name | April |
| MON | Abbreviated month name | Apr |
| MM | Month number (01-12) | 04 |
| DD | Day of month (01-31) | 05 |
| DY | Abbreviated day name | Fri |
| HH24 | Hour of day (00-23) | 14 |
| HH12 | Hour of day (01-12) | 02 |
| AM/PM | Meridian indicator | PM |
| MI | Minute (00-59) | 30 |
| SS | Second (00-59) | 45 |
| FF | Fractional seconds | 123456 |
| UUUU | ISO week-numbering year | 2024 |
| TZH:TZM | Time zone hour and minute with colon | +08:00 |
| TZH | Time zone hour | +08 |
Aliases
- DATE_FORMAT
- JSON_TO_STRING
- TO_TEXT
- TO_VARCHAR
- TO_CHAR (Oracle compatibility)
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 │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘