Skip to content

Data type conversion functions: CAST, TO_CHAR, TO_DATE, TO_NUMBER, DATE_FORMAT

View as Markdown
CAST (cast_operand AS cast_target)

The CAST operator can be used to explicitly cast one data type to another in the SELECT statement. A query list or a value expression in the WHERE clause can be cast to another data type.

Parameters

  • cast_operand: Declares the value to cast to a different data type.
  • cast_target: Specifies the type to cast to.

Return type: cast_target

Depending on the situation, data type can be automatically converted without using the CAST operator. For details, see Implicit type conversion.

See CASTing a string to date/time type regarding to convert the string of date/time type into date/time type.

The following table shows a summary of explicit type conversions (casts) using the CAST operator in CoraDB.

From \ To EN AN VC FC VB FB ENUM BLOB CLOB D T UT UTZ DT DTZ S MS SQ
EN Yes Yes Yes Yes No No No No No No No Yes Yes No No No No No
AN Yes Yes Yes Yes No No No No No No No Yes Yes No No No No No
VC Yes Yes Yes(*) Yes(*) Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No
FC Yes Yes Yes(*) Yes(*) Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No
VB No No Yes Yes Yes Yes No Yes Yes No No No No No No No No No
FB No No Yes Yes Yes Yes No Yes Yes No No No No No No No No No
ENUM No No Yes Yes No No Yes No No No No No No No No No No No
BLOB No No No No Yes Yes Yes Yes No No No No No No No No No No
CLOB No No Yes Yes No No Yes No Yes No No No No No No No No No
D No No Yes Yes No No No No No Yes No Yes Yes Yes Yes No No No
T No No Yes Yes No No No No No No Yes No No No No No No No
UT No No Yes Yes No No No No No Yes Yes Yes Yes Yes Yes No No No
UTZ No No Yes Yes No No No No No Yes Yes Yes Yes Yes Yes No No No
DT No No Yes Yes No No No No No Yes Yes Yes Yes Yes Yes No No No
DTZ No No Yes Yes No No No No No Yes Yes Yes Yes Yes Yes No No No
S No No No No No No No No No No No No No No No Yes Yes Yes
MS No No No No No No No No No No No No No No No Yes Yes Yes
SQ No No No No No No No No No No No No No No No Yes Yes Yes

(): TheCAST* operation is allowed only when the value expression and the data type to be cast have the same character set.

  • Data type key
    • EN: Exact numeric data type (INTEGER, SMALLINT, BIGINT, NUMERIC, DECIMAL)
    • AN: Approximate numeric data type (FLOAT/REAL, DOUBLE)
    • VC: Variable-length character string (VARCHAR (n))
    • FC: Fixed-length character string (CHAR (n))
    • VB: Variable-length bit string (BIT VARYING (n))
    • FB: Fixed-length bit string (BIT (n))
    • ENUM: ENUM type
    • BLOB: Binary data that is stored outside DB
    • CLOB: String data that is stored inside DB
    • D: DATE
    • T: TIME
    • DT: DATETIME
    • DTZ: DATETIME WITH TIME ZONE and DATETIME WITH LOCAL TIME ZONE data types
    • UT: TIMESTAMP
    • UTZ: TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE data types
    • S: SET
    • MS: MULTISET
    • SQ: LIST (= SEQUENCE)
--operation after casting character as INT type returns 2
SELECT (1+CAST ('1' AS INT));
(1+CAST ('1' AS INT))
2
--cannot cast the string which is out of range as SMALLINT
SELECT (1+CAST('1234567890' AS SMALLINT));
ERROR: Data overflow on data type "smallint".
--operation after casting returns 1+1234567890
SELECT (1+CAST('1234567890' AS INT));
(1+CAST('1234567890' AS INT))
1234567891
--'1234.567890' is casted to 1235 after rounding up
SELECT (1+CAST('1234.567890' AS INT));
(1+CAST('1234.567890' AS INT))
1236
--'1234.567890' is casted to string containing only first 5 letters.
SELECT (CAST('1234.567890' AS CHAR(5)));
(CAST('1234.567890' AS CHAR(5)))
1234.
--numeric type can be casted to CHAR type only when enough length is specified
SELECT (CAST(1234.567890 AS CHAR(5)));
ERROR: Data overflow on data type "character".
--numeric type can be casted to CHAR type only when enough length is specified
SELECT (CAST(1234.567890 AS CHAR(11)));
(CAST(1234.567890 AS CHAR(11)))
1234.567890
--numeric type can be casted to CHAR type only when enough length is specified
SELECT (CAST(1234.567890 AS VARCHAR));
(CAST(1234.567890 AS VARCHAR))
1234.567890
--string can be casted to time/date types only when its literal is correctly specified
SELECT (CAST('2008-12-25 10:30:20' AS TIMESTAMP));
(CAST('2008-12-25 10:30:20' AS TIMESTAMP))
2008-12-25 10:30:20
SELECT (CAST('10:30:20' AS TIME));
(CAST('10:30:20' AS TIME))
10:30:20
--string can be casted to TIME type when its literal is same as TIME's.
SELECT (CAST('2008-12-25 10:30:20' AS TIME));
(CAST('2008-12-25 10:30:20' AS TIME))
10:30:20
--string can be casted to TIME type after specifying its type of the string
SELECT (CAST(TIMESTAMP'2008-12-25 10:30:20' AS TIME));
(CAST(TIMESTAMP'2008-12-25 10:30:20' AS TIME))
10:30:20

A string cast to BLOB is stored as the bytes of its characters. BLOB_TO_BIT shows them:

SELECT CAST(BLOB_TO_BIT(CAST('1A' AS BLOB)) AS VARCHAR(100)), BLOB_LENGTH(CAST('abcde' AS BLOB));
CAST(BLOB_TO_BIT(CAST('1A' AS BLOB)) AS VARCHAR(100)) BLOB_LENGTH(CAST('abcde' AS BLOB))
3141 5

A bit string cast to a character string gives its hexadecimal digits:

SELECT CAST(B'11010000' AS VARCHAR(10));
CAST(B'11010000' AS VARCHAR(10))
d0

A number cast to TIMESTAMP is taken as a Unix time (seconds since 1970-01-01 00:00:00 UTC) and shown in the session time zone. In a session whose time zone is UTC:

SET TIME ZONE 'UTC';
SELECT CAST(1 AS TIMESTAMP), CAST(1.2F AS TIMESTAMP);
cast(1 as timestamp) cast(1.2F as timestamp)
===============================================
1970-01-01 00:00:01 1970-01-01 00:00:01

Numbers cannot be cast to DATETIME, and date/time values cannot be cast to numbers; use UNIX_TIMESTAMP for the opposite conversion:

SELECT CAST(1 AS DATETIME);
SELECT CAST(TIMESTAMP'1970-01-01 00:00:01' AS INT);
ERROR: Cannot coerce 1 to type datetime.
ERROR: Cannot coerce timestamp '1970-01-01 00:00:01' to type integer.
DATE_FORMAT (date, format)

The DATE_FORMAT function converts the value of date/time data type which include a date to specified date/time format and then return the value with the VARCHAR data type. For the format parameter to assign, refer to Date/time format 2 table of the DATE_FORMAT. The Date/time format 2 table is used in DATE_FORMAT, TIME_FORMAT, and STR_TO_DATE functions.

Parameters

  • date: A value of DATE, TIMESTAMP, DATETIME, DATETIMETZ, DATETIMELTZ, TIMESTAMPTZ, or TIMESTAMP.
  • format: Specifies the output format. The format specifier starting with '%' is used.

Return type: STRING

When the format argument is assigned, the string is interpreted according to the specified language. At that time, the language specified as the intl_date_lang system parameter is applied. If the intl_date_lang value is not set, the language specified when creating DB is applied.

For example, when the language is "de_DE" and the format is "%d %M %Y", the string "3 Oktober 2009" is interpreted as the DATE type of "2009-10-03". When the specified format argument does not correspond to the given string, an error is returned.

In the following Date/time format 2 table, the month/day, date, and AM/PM in characters are different by language.

Date/time format 2

format value Meaning
%a Weekday, English abbreviation (Sun, ... , Sat)
%b Month, English abbreviation (Jan, ... , Dec)
%c Month (1, ... , 12)
%D Day of the month, English ordinal number (1st, 2nd, 3rd, ...)
%d Day of the month, two-digit number (01, ... , 31)
%e Day of the month (1, ... , 31)
%f Microseconds, three-digit number (000, ... , 999)
%H Hour, 24-hour based, number with at least two--digit (00, ... , 23, ... , 100, ... )
%h Hour, 12-hour based two-digit number (01, ... , 12)
%I Hour, 12-hour based two-digit number (01, ... , 12)
%i Minutes, two-digit number (00, ... , 59)
%j Day of year, three-digit number (001, ... , 366)
%k Hour, 24-hour based, number with at least one-digit (0, ... , 23, ... , 100, ... )
%l Hour, 12-hour based (1, ... , 12)
%M Month, English string (January, ... , December)
%m Month, two-digit number (01, ... , 12)
%p AM or PM
%r Time, 12-hour based, hour:minute:second (hh:mi:ss AM or hh:mi:ss PM)
%S Seconds, two-digit number (00, ... , 59)
%s Seconds, two-digit number (00, ... , 59)
%T Time, 24-hour based, hour:minute:second (hh:mi:ss)
%U Week, two-digit number, week number of the year with Sunday being the first day Week (00, ... , 53)
%u Week, two-digit number, week number of the year with Monday being the first day (00, ... , 53)
%V Week, two-digit number, week number of the year with Sunday being the first day Week (00, ... , 53) (Available to use in combination with %X)
%v Week, two-digit number, week number of the year with Monday being the first day (00, ... , 53) (Available to use in combination with %x)
%W Weekday, English string (Sunday, ... , Saturday)
%w Day of the week, number index (0=Sunday, ... , 6=Saturday)
%X Year, four-digit number calculated as the week number with Sunday being the first day of the week (0000, ... , 9999) (Available to use in combination with %V)
%x Year, four-digit number calculated as the week number with Monday being the first day of the week (0000, ... , 9999) (Available to use in combination with %v)
%Y Year, four-digit number (0001, ... , 9999)
%y Year, two-digit number (00, 01, ... , 99)
%% Output the special character "%" as a string
%x Output an arbitrary character x as a string out of English letters that are not used as format specifiers.
%TZR Time zone region information. e.g. US/Pacific.
%TZD Daylight saving information. e.g. KST, KT, EET
%TZH Timezone hour offset. e.g. +09, -09
%TZM Timezone minute offset. e.g. +00, +30

The following example shows the case when the system parameter intl_date_lang is "en_US".

SELECT DATE_FORMAT(datetime'2009-10-04 22:23:00', '%W %M %Y');
DATE_FORMAT(datetime'2009-10-04 22:23:00', '%W %M %Y')
Sunday October 2009
SELECT DATE_FORMAT(datetime'2007-10-04 22:23:00', '%H:%i:%s');
DATE_FORMAT(datetime'2007-10-04 22:23:00', '%H:%i:%s')
22:23:00
SELECT DATE_FORMAT(datetime'1900-10-04 22:23:00', '%D %y %a %d %m %b %j');
DATE_FORMAT(datetime'1900-10-04 22:23:00', '%D %y %a %d %m %b %j')
4th 00 Thu 04 10 Oct 277
SELECT DATE_FORMAT(date'1999-01-01', '%X %V');
DATE_FORMAT(date'1999-01-01', '%X %V')
1998 52

With intl_date_lang set to Turkish (tr_TR), the names of days and months are Turkish:

SET SYSTEM PARAMETERS 'intl_date_lang="tr_TR"';
SELECT DATE_FORMAT(datetime'2009-10-04 22:23:00', '%W %M %Y'), DATE_FORMAT(datetime'1900-10-04 22:23:00', '%D %y %a %d %m %b %j');
Pazar Ekim 2009 4 00 Pe 04 10 Ekm 277
SELECT DATE_FORMAT(datetime'1900-10-04 22:23:00', '%D %y %a %d %m %b %j');
DATE_FORMAT(datetime'1900-10-04 22:23:00', '%D %y %a %d %m %b %j')
4th 00 Thu 04 10 Oct 277

The following example outputs the value of DATETIMETZ type which includes timezone information as the desired format.

SELECT DATE_FORMAT(datetimetz'2012-02-02 10:10:10 Europe/Zurich CET', '%TZR %TZD %TZH %TZM');
DATE_FORMAT(datetimetz'2012-02-02 10:10:10 Europe/Zurich CET', '%TZR %TZD %TZH %TZM')
Europe/Zurich CET +01 00
FORMAT ( x , dec )

The FORMAT function displays the number x by using digit grouping symbol as a thousands separator, so that its format becomes '#,###,###.#####' and performs rounding after the decimal point symbol to express as many as dec digits after it. The return value is a VARCHAR type.

Parameters

  • x: An expression that returns a numeric value
  • dec: the number of digits of fractional parts

Return type: STRING

Thousands separator symbol and decimal point symbol is output in the format according to the specified language. The language is specified by the intl_number_lang system parameter. If the value of intl_number_lang is not set, the language specified when creating DB is applied.

For example, when the language is one of the European languages, such as "de_DE" or "fr_FR", "." is interpreted as the thousands separator and "," as the decimal point symbol (see Default output of number by language of the TO_CHAR).

The following example shows command execution by setting the value of the intl_number_lang system parameter to "en_US".

SELECT FORMAT(12000.123456, 3), FORMAT(12000.123456, 0);
FORMAT(12000.123456, 3) FORMAT(12000.123456, 0)
12,000.123 12,000
With intl_number_lang set to Turkish (tr_TR), "." is the thousands separator and "," the decimal point:
SET SYSTEM PARAMETERS 'intl_number_lang="tr_TR"';
SELECT FORMAT(12000.123456, 3), FORMAT(12000.123456, 0);
12.000,123 12.000
STR_TO_DATE (string, format)

The STR_TO_DATE function converts the given character string to a date/time value by interpreting it according to the specified format and operates in the opposite way to the DATE_FORMAT function. The return value is determined by the date/time part included in the character string.

Parameters

  • string: String.
  • format: Specifies the format to interpret the character string. You should use character strings including % for the format specifiers. See Date/time format 2 table of DATE_FORMAT function.

Return type: DATETIME, DATE, TIME, DATETIMETZ

For the format argument to assign, see Date/time format 2 table of the DATE_FORMAT.

If string is invalid date/time value or format is invalid, it returns an error.

When the format argument is assigned, the string is interpreted according to the specified language. At that time, the language specified as the intl_date_lang system parameter is applied. If the intl_date_lang value is not set, the language specified when creating DB is applied.

For example, when the language is "de_DE" and the format is "%d %M %Y", the string "3 Oktober 2009" is interpreted as the DATE type of "2009-10-03". If the format argument does not correspond to the given string, an error is returned.

0 is not allowed in the argument value corresponding to year, month, and day; however, if 0 is inputted in every argument value corresponding to date and time, the value of DATE or DATETIME type that has 0 for every date and time value is returned as an exception. Note that operation in JDBC program is determined by the configuration of zeroDateTimeBehavior, connection URL property. For more information about zeroDateTimeBehavior, please see Configuration connection.

The following example shows the case when the system parameter intl_date_lang is "en_US".

SET SYSTEM PARAMETERS 'intl_date_lang="en_US"';
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');
05/01/2013
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
STR_TO_DATE('May 1, 2013','%M %d,%Y')
2013-05-01
SELECT STR_TO_DATE('13:30:17','%H:%i');
STR_TO_DATE('13:30:17','%H:%i')
13:30:00
SELECT STR_TO_DATE('09:30:17 PM','%r');
STR_TO_DATE('09:30:17 PM','%r')
21:30:17
SELECT STR_TO_DATE('0,0,0000','%d,%m,%Y');
STR_TO_DATE('0,0,0000','%d,%m,%Y')
0000-00-00

With intl_date_lang set to tr_TR, the Turkish month name Ekim is read as October:

SET SYSTEM PARAMETERS 'intl_date_lang="tr_TR"';
SELECT STR_TO_DATE('3 Ekim 2009', '%d %M %Y');
10/03/2009

The following example shows which converts a date/time string with timezone information into DATETIMETZ type value.

SELECT STR_TO_DATE('2001-10-11 02:03:04 AM Europe/Bucharest EEST', '%Y-%m-%d %h:%i:%s %p %TZR %TZD');
STR_TO_DATE('2001-10-11 02:03:04 AM Europe/Bucharest EEST', '%Y-%m-%d %h:%i:%s %p %TZR %TZD')
2001-10-11 02:03:04.000 Europe/Bucharest EEST
TIME_FORMAT (time, format)

The TIME_FORMAT function converts the date/time data type value including time value into a string of specified date/time format, and returns the value with the VARCHAR data type.

Parameters

  • time: A value of a type with time. (TIME, TIMESTAMP, DATETIME, TIMESTAMPTZ or DATETIMETZ)
  • format: Specifies the output format. Use a string that contains '%' as a specifier. See the table Date/time format 2 of DATE_FORMAT function.

Return type: STRING

When the format argument is assigned, the time is output according to the specified language. At this time, the language specified as the intl_date_lang system parameter is applied. If intl_date_lang system parameter is not set, the language specified when creating DB is applied.

For example, when the language is set to "de_DE" and the format is "%h:%i:%s %p", "08:46:53 PM" is output as "08:46:53 Nachm.". When the format argument specified does not correspond to the given string, an error is returned.

The following example shows the case when the system parameter intl_date_lang is "en_US".

SET SYSTEM PARAMETERS 'intl_date_lang="en_US"';
SELECT TIME_FORMAT(time'22:23:00', '%H %i %s');
'22 23 00'
SELECT TIME_FORMAT(time'23:59:00', '%H %h %i %s %f');
TIME_FORMAT(time'23:59:00', '%H %h %i %s %f')
23 11 59 00 000
SELECT TIME_FORMAT(time'20:46:53', '%h:%i:%s %p');
TIME_FORMAT(time'20:46:53', '%h:%i:%s %p')
08:46:53 PM
The following outputs the value with a timezone information into a specified format string.
SELECT TIME_FORMAT(datetimetz'2001-10-11 02:03:04 AM Europe/Bucharest EEST', '%h:%i:%s %p %TZR %TZD');
TIME_FORMAT(datetimetz'2001-10-11 02:03:04 AM Europe/Bucharest EEST', '%h:%i:%s %p %TZR %TZD')
02:03:04 AM Europe/Bucharest EEST

TO_CHAR ( date_time [, format[, date_lang_string_literal ]] )

The TO_CHAR (date_time) function converts the value of date/time types (TIME, DATE, TIMESTAMP, DATETIME) to a string depending on the table Date/time format 1 and then returns the value. The type of the return value is VARCHAR.

Parameters

  • date_time: A value of date/time type. (TIME, DATE, TIMESTAMP, DATETIME, DATETIMETZ, DATETIMELTZ, TIMESTAMPTZ, TIMESTAMPLTZ)
  • format: A format of return value.
  • date_lang_string_literal: Specifies a language applied to a return value.

Return type: STRING

When the format argument is specified, the date_time is output according to the specified language (see the Date/time format 1 table). A language is defined by the date_lang_string_literal. If date_lang_string_literal is omitted, the language specified by the intl_date_lang parameter is applied; if the value of intl_date_lang is not specified, the language specified when creating DB is applied.

For example, when the language is set to "de_DE" and the format is "HH:MI:SS:AM", "08:46:53 PM" is output as "08:46:53 Nachm.". When the format argument specified does not correspond to the given string, an error is returned.

When the format argument is omitted, the date_time is output as a string according to the default output format of the "en_US"(see the following table Default date/time output format for each language).

Default date/time output format for each language

LANG DATE TIME TIMESTAMP DATETIME TIMESTAMP WITH TIME ZONE DATETIME WITH TIME ZONE
en_US 'MM/DD/YYYY' 'HH:MI:SS AM' 'HH:MI:SS AM MM/DD/YYYY' 'HH:MI:SS.FF AM MM/DD/YYYY' 'HH:MI:SS AM MM/DD/YYYY TZR' 'HH:MI:SS.FF AM MM/DD/YYYY TZR'
de_DE 'DD.MM.YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD.MM.YYYY' 'HH24:MI:SS.FF DD.MM.YYYY' 'HH24:MI:SS DD.MM.YYYY TZR' 'HH24:MI:SS.FF DD.MM.YYYY TZR'
es_ES 'DD.MM.YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD.MM.YYYY' 'HH24:MI:SS.FF DD.MM.YYYY' 'HH24:MI:SS DD/MM/YYYY TZR' 'HH24:MI:SS.FF DD/MM/YYYY TZR'
fr_FR 'DD.MM.YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD.MM.YYYY' 'HH24:MI:SS.FF DD.MM.YYYY' 'HH24:MI:SS DD/MM/YYYY TZR' 'HH24:MI:SS.FF DD/MM/YYYY TZR'
it_IT 'DD.MM.YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD.MM.YYYY' 'HH24:MI:SS.FF DD.MM.YYYY' 'HH24:MI:SS DD/MM/YYYY TZR' 'HH24:MI:SS.FF DD/MM/YYYY TZR'
ja_JP 'YYYY/MM/DD' 'HH24:MI:SS' 'HH24:MI:SS YYYY/MM/DD' 'HH24:MI:SS.FF YYYY/MM/DD' 'HH24:MI:SS YYYY/MM/DD TZR' 'HH24:MI:SS.FF YYYY/MM/DD TZR'
km_KH 'DD/MM/YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD/MM/YYYY' 'HH24:MI:SS.FF DD/MM/YYYY' 'HH24:MI:SS DD/MM/YYYY TZR' 'HH24:MI:SS.FF DD/MM/YYYY TZR'
ko_KR 'YYYY.MM.DD' 'HH24:MI:SS' 'HH24:MI:SS YYYY.MM.DD' 'HH24:MI:SS.FF YYYY.MM.DD' 'HH24:MI:SS YYYY.MM.DD TZR' 'HH24:MI:SS.FF YYYY.MM.DD TZR'
tr_TR 'DD.MM.YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD.MM.YYYY' 'HH24:MI:SS.FF DD.MM.YYYY' 'HH24:MI:SS DD.MM.YYYY TZR' 'HH24:MI:SS.FF DD.MM.YYYY TZR'
vi_VN 'DD/MM/YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD/MM/YYYY' 'HH24:MI:SS.FF DD/MM/YYYY' 'HH24:MI:SS DD/MM/YYYY TZR' 'HH24:MI:SS.FF DD/MM/YYYY TZR'
zh_CN 'YYYY-MM-DD' 'HH24:MI:SS' 'HH24:MI:SS YYYY-MM-DD' 'HH24:MI:SS.FF YYYY-MM-DD' 'HH24:MI:SS YYYY-MM-DD TZR' 'HH24:MI:SS.FF YYYY-MM-DD TZR'
ro_RO 'DD.MM.YYYY' 'HH24:MI:SS' 'HH24:MI:SS DD.MM.YYYY' 'HH24:MI:SS.FF DD.MM.YYYY' 'HH24:MI:SS DD.MM.YYYY TZR' 'HH24:MI:SS.FF DD.MM.YYYY TZR'

Date/time format 1

Format element Description
CC Century
YYYY , YY Year with 4 numbers, Year with 2 numbers
Q Quarter (1, 2, 3, 4; January - March = 1)
MM Month (01-12; January = 01) Note: MI represents the minute of hour.
MONTH Month in characters
MON Abbreviated month name
DD Day (1 - 31)
DAY Day of the week in characters
DY Abbreviated day of the week
D or d Day of the week in numbers (1 - 7)
AM or PM AM/PM
A.M. or P.M. AM/PM with periods
HH or HH12 Hour (1 -12)
HH24 Hour (0 - 23)
MI Minute (0 - 59)
SS Second (0 - 59)
FF Millisecond (0-999)
- / , . ; : "text" Punctuation and quotation marks are represented as they are in the result
TZD Daylight saving information. e.g. KST, KT, EET
TZH Timezone hour offset. e.g. +09, -09
TZM Timezone minute offset. e.g. +00, +30

Example of date_lang_string_literal

Format element date_lang_string_literal
'en_US' 'ko_KR'
MONTH JANUARY 1월
MON JAN 1
DAY MONDAY 월요일
DY MON 월
Month January 1월
Mon Jan 1
Day Monday 월요일
Dy Mon 월
month january 1월
mon jan 1
day monday 월요일
Dy mon 월
AM AM 오전
Am Am 오전
am am 오전
A.M. A.M. 오후
A.m. A.m. 오전
a.m. a.m. 오전
PM PM 오후
Pm Pm 오후
pm pm 오후
P.M. P.M. 오후
P.m. P.m. 오후
p.m. p.m. 오후

Example of format digits of return value

Format element en_US digits ko_KR digits
MONTH(Month, month) 9 4
MON(Mon, mon) 3 2
DAY(Day, day) 9 6
DY(Dy, dy) 3 2
HH12, HH24 2 2
"text" The length of the text The length of the text
Other formats Same as the length of the format Same as the length of the format

Formatting order dates:

SELECT id, TO_CHAR(order_date, 'DD, DY, MON, YYYY') AS d1, TO_CHAR(order_date, 'Day, DD Month YYYY') AS d2 FROM orders WHERE id < 3 ORDER BY id;
id d1 d2
0 07, SUN, NOV, 2021 Sunday , 07 November 2021
1 19, SUN, OCT, 2025 Sunday , 19 October 2025
2 05, MON, SEP, 2022 Monday , 05 September 2022

Day and month names are padded to the length of the longest name. A DATETIME value, the date since when a brand has manufactured a product:

SELECT TO_CHAR(m.since, 'HH12:MI:SS:FF pm, YYYY-MM-DD-DAY') AS s1, TO_CHAR(m.since, 'HH24:MI, DD, MONTH, YYYY') AS s2
FROM MATCH (b:brand)-[m:manufactures]->(p:product) WHERE p.id = 1618;
s1 s2
06:22:04:000 am, 2013-02-28-THURSDAY 06:22, 28, FEBRUARY , 2013
SELECT TO_CHAR(TIMESTAMP'2009-10-04 22:23:00', 'Day Month yyyy');
TO_CHAR(TIMESTAMP'2009-10-04 22:23:00', 'Day Month yyyy')
Sunday October 2009

The third argument selects the language of the names:

SELECT TO_CHAR(order_date, 'Day Month YYYY', 'tr_TR') AS tr, TO_CHAR(order_date, 'Day Month YYYY') AS en FROM orders WHERE id = 0;
tr en
Pazar Kasım 2021 Sunday November 2021

The following is an example to output date/time type with timezone in TO_CHAR function.

If you don't define a format, it outputs as the following format

SELECT TO_CHAR(datetimetz'2001-10-11 02:03:04 AM Europe/Bucharest EEST');
TO_CHAR(datetimetz'2001-10-11 02:03:04 AM Europe/Bucharest EEST')
2001-10-11 02:03:04.000 Europe/Bucharest EEST

If you define a format, it outputs as the defined format.

SELECT TO_CHAR(datetimetz'2001-10-11 02:03:04 AM Europe/Bucharest EEST', 'MM/DD/YYYY HH24:MI TZR TZD TZH TZM');
TO_CHAR(datetimetz'2001-10-11 02:03:04 AM Europe/Bucharest EEST', 'MM/DD/YYYY HH24:MI TZR TZD TZH TZM')
10/11/2001 02:03 Europe/Bucharest EEST +03 00

TO_CHAR(number[, format[, number_lang_string_literal ] ])

The TO_CHAR function converts a numeric data type to a character string according to Number format and returns it. The type of the return value is VARCHAR .

Parameters

  • number: Specifies an expression that returns numeric data type string. If the input value is NULL, NULL is returned. If the input value is character type, the character itself is returned.
  • format: Specifies a format of return value. If format is not specified, all significant figures are returned as character string by default. If the value is NULL, NULL is returned.
  • number_lang_string_literal: Specifies the language to be applied to the input value.

Return type: STRING

If format argument is specified, number is converted into a character string according to a specified language. At this time, the language is defined by the number_lang_string_literal argument. If number_lang_string_literal is omitted, the language specified by intl_number_lang system parameter is applied; if intl_number_lang is not set, the language specified when creating DB is applied.

For example, if the language is one of the European languages, such as "de_DE" or "fr_FR", "." is printed out as a thousands separator and "," is printed out as a decimal point. If the format argument does not correspond to the given string, the function returns an error.

If format argument is omitted, number is converted into a character string according to the default format of a specified language (see the table Default output of number for each language).

Number format

Format element Example Description
9 9999 The number of 9's represents the number of significant figures to be returned. If the number of significant figures specified in the format is not sufficient, only the decimal part is rounded. If it is less than the number of digits in an integer, # is outputted. If the number of significant figures specified in the format is sufficient, the part preceding the integer part is filled with space characters and the decimal part is filled with 0.
0 0999 If the number of significant figures specified in the format is sufficient, the part preceding the integer part is filled with 0, not space characters before the value is returned.
S S9999 Outputs the negative/positive sign in the specified position. These signs can be used only at the beginning of character string.
C C9999 Returns the ISO currency code at the specified position.
, (comma) 9,999 Returns a comma (",") at the specified position. Multiple commas are allowed in the format.
. (decimal point) 9.999 Returns a decimal point (".") at the specified position, separating the integer part from the fractional part. Only one decimal point is allowed in the format. See Default output of number for each language.
EEEE 9.99EEEE Returns a scientific notation number.

Default output of number for each language

Language Locale Digit grouping symbol Decimal symbol Example
English en_US ,(comma) .(period) 123,456,789.012
Korean ko_KR ,(comma) .(period) 123,456,789.012
Turkish tr_TR .(period) ,(comma) 123.456.789,012

The results below are shown in quotes, because number formats pad the result with leading spaces. With the default number language (en_US):

SELECT TO_CHAR(12345, 'S999999'), TO_CHAR(12345, 'S099999'), TO_CHAR(1234567, '9,999,999,999'), TO_CHAR(1234567, '9.999.999.999');
' +12345' '+012345' ' 1,234,567' '#############'

A format that is too short for the number, or that uses the wrong separators for the language, produces # characters.

SELECT TO_CHAR(123.4567, '99'), TO_CHAR(123.4567, '999.99999'), TO_CHAR(123.4567, '99999.999'), TO_CHAR(1.234567, '99.999EEEE');
'##' '123.45670' ' 123.457' '1.235E+00'

With the Turkish number format (tr_TR), "." groups thousands and "," is the decimal point. The language can be given as the third argument, or set for the session with intl_number_lang:

SELECT TO_CHAR(1234567, '9.999.999.999', 'tr_TR'), TO_CHAR(123.4567, '999,99999', 'tr_TR'), TO_CHAR(1.234567, '99,999EEEE', 'tr_TR');
' 1.234.567' '123,45670' '1,235E+00'
SET SYSTEM PARAMETERS 'intl_number_lang="tr_TR"';
SELECT TO_CHAR(123.4567);
'123,4567'
TO_DATE(string [,format [,date_lang_string_literal]])

The TO_DATE function interprets a character string based on the date format given as an argument, converts it to a DATE type value, and returns it. For the format, see Date/time format 1.

Parameters

  • string: A character string
  • format: Specifies a format of return value to be converted as DATE type. See Date/time format 1. If the value is NULL, NULL is returned.
  • date_lang_string_literal: Specifies the language for the input value to be applied.

Return type: DATE

When the format argument is specified, the string is interpreted according to the specified language. At this time, the language is set by date_lang_string_literal argument. If date_lang_string_literal argument is not set, the language is specified by the intl_date_lang system parameter; if the value of intl_date_lang is not set, the language is applied by the language specified when creating DB.

For example, when a language is "de_DE" and string is "12.mai.2012", and format is "DD.mon.YYYY", it is interpreted as May 12th, 2012.When the format parameter specified does not correspond to the given string, an error is returned.

When the format argument is omitted, string is interpreted as the CoraDB default format (refer to Recommended format for strings in date/time type) and if it fails, string is interpreted as the language format (see the table Default output format of language in the TO_CHAR) by intl_date_lang. If the value of intl_date_lang is not set, the language is applied by the language specified when creating DB.

For example, when a language is "de_DE", the acceptable strings for DATE type are "MM/DD/YYYY", CoraDB default format and "DD.MM.YYYY", "de_DE" default format.

The following example shows the query executed by the locale specified when creating DB to "en_US.utf8".

--selecting a date type value casted from a string in the specified format
SELECT TO_DATE('12/25/2008');
TO_DATE('12/25/2008')
2008-12-25
SELECT TO_DATE('25/12/2008', 'DD/MM/YYYY');
TO_DATE('25/12/2008', 'DD/MM/YYYY')
2008-12-25
SELECT TO_DATE('081225', 'YYMMDD');
TO_DATE('081225', 'YYMMDD')
2008-12-25
SELECT TO_DATE('2008-12-25', 'YYYY-MM-DD');
TO_DATE('2008-12-25', 'YYYY-MM-DD')
2008-12-25

With intl_date_lang set to tr_TR, the Turkish default format DD.MM.YYYY is accepted. The third argument gives the language for month names:

SET SYSTEM PARAMETERS 'intl_date_lang="tr_TR"';
SELECT TO_DATE('25.12.2012');
SELECT TO_DATE('12/EKM/2012', 'DD/MON/YYYY', 'tr_TR');
12/25/2012
10/12/2012
TO_DATETIME (string [,format [,date_lang_string_literal]])

The TO_DATETIME function interprets a character string based on the date-time format given as an argument, converts it to a DATETIME type value, and returns it. For the format, see Date/time format 1.

Parameters

  • string: A character string
  • format: Specifies a format of return value to be converted as DATETIME type. See the table, Date/time format 1. If the value is NULL, NULL is returned.
  • date_lang_string_literal: Specifies the language for the input value to be applied.

Return type: DATETIME

When the format argument is specified, the string is interpreted according to the specified language.

For example, when a language is "de_DE" and string is "12/mai/2012 12:10:00 Nachm." and format is "DD/MON/YYYY HH:MI:SS AM", it is interpreted as May 12th, 2012 12:10:00 PM. At this time, the language is set by date_lang_string_literal argument. If date_lang_string_literal argument is not set, the language is specified by the intl_date_lang system parameter; if the value of intl_date_lang is not set, the language is specified by the language specified when creating DB. When the format parameter specified does not correspond to the given string, an error is returned.

When the format argument is omitted, string is interpreted as the CoraDB default format (refer to Recommended format for strings in date/time type) and if it fails, string is interpreted as the language format (see the table Default output format of language in the TO_CHAR) by intl_date_lang. If the value of intl_date_lang is not set, the language is applied by the language specified when creating DB.

For example, when a language is "de_DE", the acceptable strings for DATETIME type are "HH:MI:SS.FF AM MM/DD/YYYY", CoraDB default format and "HH24:MI:SS.FF DD.MM.YYYY", "de_DE" default format.

The following examples run in a database created with the en_US locale.

--selecting a datetime type value casted from a string in the specified format
SELECT TO_DATETIME('13:10:30 12/25/2008');
TO_DATETIME('13:10:30 12/25/2008')
2008-12-25 13:10:30.000
SELECT TO_DATETIME('08-Dec-25 13:10:30.999', 'YY-Mon-DD HH24:MI:SS.FF');
TO_DATETIME('08-Dec-25 13:10:30.999', 'YY-Mon-DD HH24:MI:SS.FF')
2008-12-25 13:10:30.999
SELECT TO_DATETIME('DATE: 12-25-2008 TIME: 13:10:30.999', '"DATE:" MM-DD-YYYY "TIME:" HH24:MI:SS.FF');
TO_DATETIME('DATE: 12-25-2008 TIME: 13:10:30.999', '"DATE:" MM-DD-YYYY "TIME:" HH24:MI:SS.FF')
2008-12-25 13:10:30.999

With intl_date_lang set to tr_TR:

SET SYSTEM PARAMETERS 'intl_date_lang="tr_TR"';
SELECT TO_DATETIME('13:10:30.999 25.12.2012');
SELECT TO_DATETIME('12/EKM/2012 10:23:00 PM', 'DD/MON/YYYY HH:MI:SS AM', 'tr_TR');
01:10:30.999 PM 12/25/2012
10:23:00.000 PM 10/12/2012
TO_DATETIME_TZ (string [,format [,date_lang_string_literal]])

TO_DATETIME_TZ function is the same as TO_DATETIME function except that this function can include a timezone information on this input string.

Return type: DATETIMETZ

SELECT TO_DATETIME_TZ('13:10:30 Asia/Seoul 12/25/2008', 'HH24:MI:SS TZR MM/DD/YYYY');
TO_DATETIME_TZ('13:10:30 Asia/Seoul 12/25/2008', 'HH24:MI:SS TZR MM/DD/YYYY')
2008-12-25 13:10:30.000 Asia/Seoul KST
TO_NUMBER(string [, format ])

The TO_NUMBER function interprets a character string based on the number format given as an argument, converts it to a NUMERIC type value, and returns it.

Parameters

  • string: Specifies an expression that returns character string. If the value is NULL, NULL is returned.
  • format: Specifies a format of return value to be converted as NUMBER type. See Number format. If it is omitted, NUMERIC(38,0) value is returned.

Return type: NUMERIC

When the format argument is assigned, the string is interpreted according to the specified language. At this time, the language is specified by the intl_number_lang system parameter. If the value of intl_number_lang is not set, the language specified when creating DB is applied.

For example, when the language is one of the European languages, such as "de_DE" and "fr_FR", "." is interpreted as the cipher identifier and "," as the decimal point symbol. When the format parameter specified does not correspond to the given string, an error is returned.

If the format argument is omitted, string is interpreted according to the default output format set by intl_number_lang (see Default output of number for each language). When the intl_number_lang is not set, the language specified when creating DB is applied.

The following example shows execution of the database by setting the value of system parameter intl_number_lang as "en_US".

SET SYSTEM PARAMETERS 'intl_number_lang="en_US"';
--selecting a number casted from a string in the specified format
SELECT TO_NUMBER('-1234');
-1234
SELECT TO_NUMBER('12345','999999');
TO_NUMBER('12345','999999')
12345
SELECT TO_NUMBER('12,345.67','99,999.999');
TO_NUMBER('12,345.67','99,999.999')
12345.670
SELECT TO_NUMBER('12345.67','99999.999');
TO_NUMBER('12345.67','99999.999')
12345.670

With intl_number_lang set to tr_TR, "." groups thousands and "," is the decimal point:

SET SYSTEM PARAMETERS 'intl_number_lang="tr_TR"';
SELECT TO_NUMBER('12.345,67', '99.999,999');
12345.670
TO_TIME (string [,format [,date_lang_string_literal]])

The TO_TIME function interprets a character string based on the time format given as an argument, converts it to a TIME type value, and returns it. For the format, see Date/time format 1.

Parameters

  • string: Specifies an expression that returns character string. If the value is NULL, NULL is returned.
  • format: Specifies a format of return value to be converted as TIME type. See Date/time format 1. If the value is NULL, NULL is returned.
  • date_lang_string_literal: Specifies the language for the input value to be applied.

Return type: TIME

When the format argument is specified, the string is interpreted according to the specified language. At this time, the language is set by date_lang_string_literal argument. If date_lang_string_literal argument is not set, the language is specified by the intl_date_lang system parameter; if the value of intl_date_lang is not set, the language specified when creating DB is applied. When the format parameter does not correspond to the given string, an error is returned.

For example, when a language is "de_DE" and string is "10:23:00 Nachm.", and format is "HH/MI/SS/AM, it is interpreted as 10:23:00 PM.

When the format argument is omitted, string is interpreted as the CoraDB default format (refer to Recommended format for strings in date/time type) and if it fails, string is interpreted as the language format (see the table Default output format of language in the TO_CHAR) by intl_date_lang. If the value of intl_date_lang is not set, the language is applied by the language specified when creating DB.

For example, when a language is "de_DE", the acceptable strings for TIME type are "HH:MI:SS AM", CoraDB default format and "HH24:MI:SS", "de_DE" default format.

The following example shows execution of the database by setting the value of system parameter intl_date_lang as "en_US".

SET SYSTEM PARAMETERS 'intl_date_lang="en_US"';
--selecting a time type value casted from a string in the specified format
SELECT TO_TIME ('13:10:30');
01:10:30 PM
SELECT TO_TIME('HOUR: 13 MINUTE: 10 SECOND: 30', '"HOUR:" HH24 "MINUTE:" MI "SECOND:" SS');
TO_TIME('HOUR: 13 MINUTE: 10 SECOND: 30', '"HOUR:" HH24 "MINUTE:" MI "SECOND:" SS')
13:10:30
SELECT TO_TIME ('13:10:30', 'HH24:MI:SS');
TO_TIME ('13:10:30', 'HH24:MI:SS')
13:10:30
SELECT TO_TIME ('13:10:30', 'HH12:MI:SS');
ERROR: Conversion error in date format.

With intl_date_lang set to tr_TR, the Turkish default format HH24:MI:SS is accepted:

SET SYSTEM PARAMETERS 'intl_date_lang="tr_TR"';
SELECT TO_TIME('13:10:30');
01:10:30 PM
TO_TIMESTAMP(string [, format [,date_lang_string_literal]])

The TO_TIMESTAMP function interprets a character string based on the time format given as an argument, converts it to a TIMESTAMP type value, and returns it. For the format, see Date/time format 1.

Parameters

  • string: Specifies an expression that returns character string. If the value is NULL, NULL is returned.
  • format: Specifies a format of return value to be converted as TIMESTAMP type. See Date/time format 1. If the value is NULL, NULL is returned.
  • date_lang_string_literal: Specifies the language for the input value to be applied.

Return type: TIMESTAMP

When the format argument is specified, the string is interpreted according to the specified language. The language is set by date_lang_string_literal argument. If date_lang_string_literal argument is not set, the language is specified by the intl_date_lang system parameter; if the value of intl_date_lang is not set, the language specified when creating DB is applied.

For example, when a language is "de_DE" and string is "12/mai/2012 12:10:00 Nachm.", and format is "DD/MON/YYYY HH:MI:SS AM", it is interpreted as May 12th, 2012, 12:10:00 AM. When the format parameter specified does not correspond to the given string, an error is returned.

When the format argument is omitted, string is interpreted as the CoraDB default format(refer to Recommended format for strings in date/time type) and if it fails, string is interpreted as the language format (see the table Default output format of language in the TO_CHAR) by intl_date_lang. If the value of intl_date_lang is not set, the language is applied by the language specified when creating DB.

For example, when a language is "de_DE", the acceptable strings for TIMESTAMP type are "HH:MI:SS AM MM/DD/YYYY", CoraDB default format and "HH24:MI:SS DD.MM.YYYY", "de_DE" default format.

The following example shows execution of the database by setting the value of system parameter intl_date_lang as "en_US".

SET SYSTEM PARAMETERS 'intl_date_lang="en_US"';
--selecting a timestamp type value casted from a string in the specified format
SELECT TO_TIMESTAMP('13:10:30 12/25/2008');
01:10:30 PM 12/25/2008
SELECT TO_TIMESTAMP('08-Dec-25 13:10:30', 'YY-Mon-DD HH24:MI:SS');
TO_TIMESTAMP('08-Dec-25 13:10:30', 'YY-Mon-DD HH24:MI:SS')
2008-12-25 13:10:30
SELECT TO_TIMESTAMP('YEAR: 2008 DATE: 12-25 TIME: 13:10:30', '"YEAR:" YYYY "DATE:" MM-DD "TIME:" HH24:MI:SS');
TO_TIMESTAMP('YEAR: 2008 DATE: 12-25 TIME: 13:10:30', '"YEAR:" YYYY "DATE:" MM-DD "TIME:" HH24:MI:SS')
2008-12-25 13:10:30

With intl_date_lang set to tr_TR, the Turkish default format HH24:MI:SS DD.MM.YYYY is accepted:

SET SYSTEM PARAMETERS 'intl_date_lang="tr_TR"';
SELECT TO_TIMESTAMP('13:10:30 25.12.2008');
01:10:30 PM 12/25/2008

If the string contains only a time, the date part is the first day of the current month: TO_TIMESTAMP('10:23:00 PM', 'HH12:MI:SS AM') run in November 2026 returns 10:23:00 PM 11/01/2026.

TO_TIMESTAMP_TZ(string [, format [,date_lang_string_literal]])
**TO_TIMESTAMP_TZ** function is the same as `TO_TIMESTAMP <#to_timestamp>`__ function except that this function can include a timezone information on this input string.
:rtype: TIMESTAMPTZ
SELECT TO_TIMESTAMP_TZ('13:10:30 Asia/Seoul 12/25/2008', 'HH24:MI:SS TZR MM/DD/YYYY');
TO_TIMESTAMP_TZ('13:10:30 Asia/Seoul 12/25/2008', 'HH24:MI:SS TZR MM/DD/YYYY')
2008-12-25 13:10:30 Asia/Seoul KST