PolarDB for PostgreSQL(Compatible with Oracle) supports the nls_timestamp_format and nls_timestamp_tz_format functions.
Overview
- The
nls_timestamp_formatfunction controls the conversion format of thepg_catalog.to_char(arg1 timestamp)andpg_catalog.to_timestamp(arg1 text)functions, and the output format of thetimestampdata type. - The
nls_timestamp_tz_formatfunction controls the conversion format of thepg_catalog.to_char(arg1 timestamptz)andpg_catalog.to_timestamp_tz(arg1 text)functions, and the output format of thetimestamptzdata type.
Note
TIMESTAMPandTIMESTAMP without time zoneare of the same data type.TIMESTAMPTZandTIMESTAMPTZ with time zoneare of the same data type.
Usage
- nls_timestamp_format
The initial value of
nls_timestamp_formatis''.You can execute the
SETstatement to setnls_timestamp_formatto a supportedTIMESTAMPdata type. If the format is invalid, an error is reported.You can reset the value by executing the
RESET nls_timestamp_format;statement.Examples of theYYYY/MM/DD HH24:MI:SSformat:- Set
nls_timestamp_formattoYYYY/MM/DD HH24:MI:SS.
Sample result:set nls_timestamp_format to 'YYYY/MM/DD HH24:MI:SS';set - Convert
2021-11-11 11:11:11in theYYYY/MM/DD HH24:MI:SSformat and then run the to_char function.
Sample result:select to_char('2021-11-11 11:11:11'::timestamp);to_char --------------------- 2021/11/11 11:11:11 (1 row) - Convert
2021-11-11 11:11:11in theYYYY/MM/DD HH24:MI:SSformat and then run the to_timestamp function.
Sample result:select to_timestamp('2021/11/11 11:11:11'::text);to_timestamp --------------------- 2021/11/11 11:11:11 (1 row) - Display the
timestampdata type in theYYYY/MM/DD HH24:MI:SSformat.
Sample result:select '2021-11-11 11:11:11'::timestamp;timestamp --------------------- 2021/11/11 11:11:11 (1 row)
Note The format ofarg1into_timestamp(arg1 TEXT)must exactly the same as the format specified bynls_timestamp_format. Otherwise, an error or an unexpected result is returned.# Set nls_timestamp_format to YYYY/MM/DD HH:MI:SS.FF. set nls_timestamp_format = 'YYYY/MM/DD HH:MI:SS.FF'; # Sample result: set # Convert 2000-03-28 08:00:00 in the YYYY/MM/DD HH:MI:SS.FF format and then run the to_timestamp function. select to_timestamp('2000-03-28 08:00:00'); # Sample result: ERROR: date/time field value out of range: "2000-03-28 08:00:00" CONTEXT: PL/pgSQL function to_timestamp(text) line 7 at RETURN - Set
- nls_timestamp_tz_format
The initial value of the
nls_timestamp_tz_formatfunction is''.You can execute the
SETstatement to setnls_timestamp_tz_formatto a supportedTIMESTAMPTZdata type. If the format is invalid, an error is reported.You can reset the value by executing the
reset nls_timestamp_tz_format;statement.Examples of theYYYY/MM/DD HH24:MI:DD TZH:TZMformat:- Set
nls_timestamp_formattoYYYY/MM/DD HH24:MI:DD TZH:TZM.
Sample result:set nls_timestamp_tz_format to 'YYYY/MM/DD HH24:MI:SS TZH:TZM';set - Convert
2021-11-11 11:11:11 +8in theYYYY/MM/DD HH24:MI:DD TZH:TZMformat and then run the to_char function.
Sample result:select to_char('2021-11-11 11:11:11 +8'::timestamptz);to_char ---------------------------- 2021/11/11 03:11:11 +00:00 (1 row) - Convert
2021/11/11 11:11:11 +8in theYYYY/MM/DD HH24:MI:DD TZH:TZMformat and then run the to_timestamp function.
Sample result:select to_timestamp_tz('2021/11/11 11:11:11 +8'::text);to_timestamp_tz ---------------------------- 2021/11/11 03:11:11 +00:00 (1 row) - Display the
timestamptzdata type in theYYYY/MM/DD HH24:MI:DD TZH:TZMformat.
Sample result:select '2021/11/11 11:11:11 +8'::timestamptztimestamptz ---------------------------- 2021/11/11 03:11:11 +00:00 (1 row)
Note The format ofarg1into_timestamp_tz(arg1 TEXT)must exactly the same as the format specified bynls_timestamp_format. Otherwise, an error or an unexpected result is returned.# Set nls_timestamp_tz_format to YYYY/MM/DD HH:MI:SS.FF TZH:TZM. set nls_timestamp_tz_format = 'YYYY/MM/DD HH:MI:SS.FF TZH:TZM'; # Sample result: set # Convert 2000-03-28 08:00:00 +8 in the YYYY/MM/DD HH:MI:SS.FF TZH:TZM format and then execute the to_timestamp function. select to_timestamp('2000-03-28 08:00:00 +8'); # Sample result: ERROR: date/time field value out of range: "2000-03-28 08:00:00 +8" CONTEXT: PL/pgSQL function to_timestamp(text) line 7 at RETURN - Set