From 3728819127360c03b690ebebdd6fde353a3c2049 Mon Sep 17 00:00:00 2001 From: fengyang Date: Wed, 25 Sep 2024 15:36:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20to\=5Ftimestamp=20?= =?UTF-8?q?=E6=96=B0=E8=AF=AD=E6=B3=95=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../en/docs/BriefTutorial/functions.md | 93 +++++++++++++++++ .../type-conversion-functions.md | 99 +++++++++++++++++++ .../SQLReference/type-conversion-functions.md | 97 ++++++++++++++++++ .../\345\207\275\346\225\260.md" | 90 +++++++++++++++++ ...54\346\215\242\345\207\275\346\225\260.md" | 99 +++++++++++++++++++ content/en/docs/BriefTutorial/functions.md | 93 +++++++++++++++++ .../\345\207\275\346\225\260.md" | 91 +++++++++++++++++ ...54\346\215\242\345\207\275\346\225\260.md" | 99 +++++++++++++++++++ 8 files changed, 761 insertions(+) diff --git a/content/docs-lite/en/docs/BriefTutorial/functions.md b/content/docs-lite/en/docs/BriefTutorial/functions.md index 72f89e71e..04f9eea70 100644 --- a/content/docs-lite/en/docs/BriefTutorial/functions.md +++ b/content/docs-lite/en/docs/BriefTutorial/functions.md @@ -1149,4 +1149,97 @@ The common functions of openGauss are as follows: (1 row) ``` +- to\_timestamp \( string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + Description: + Converts values of the string type into the timestamp of the specified type. + + DEFAULT return_value ON CONVERSION ERROR is optional. Allows you to specify the value to be returned when a conversion error occurs. + + fmt is optional. Format string. + + Return type: timestamp without time zone + + Example: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + diff --git a/content/docs-lite/en/docs/BriefTutorial/type-conversion-functions.md b/content/docs-lite/en/docs/BriefTutorial/type-conversion-functions.md index b0bfb3b27..9c6cdbae1 100644 --- a/content/docs-lite/en/docs/BriefTutorial/type-conversion-functions.md +++ b/content/docs-lite/en/docs/BriefTutorial/type-conversion-functions.md @@ -512,6 +512,105 @@ ``` +- to\_timestamp\(string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + Description: Converts a string into a value of the timestamp type according to the format specified by **fmt**. When **fmt** is not specified, perform the conversion according to the format specified by **nls\_timestamp\_format**. + + Returns the value of return_value when a conversion error occurs. + + In **to\_timestamp** in openGauss, + + - If the input year *YYYY* is 0, an error will be reported. + - If the input year *YYYY* is less than 0, specify *SYYYY* in **fmt**. The year with the value of n \(an absolute value\) BC will be output correctly. + + Characters in the **fmt** must match the schema for formatting the data and time. Otherwise, an error is reported. + + Return type: timestamp without time zone + + Example: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + + + **Table 1** Template patterns for numeric formatting diff --git a/content/docs-lite/en/docs/SQLReference/type-conversion-functions.md b/content/docs-lite/en/docs/SQLReference/type-conversion-functions.md index 32240d591..0794f27ef 100644 --- a/content/docs-lite/en/docs/SQLReference/type-conversion-functions.md +++ b/content/docs-lite/en/docs/SQLReference/type-conversion-functions.md @@ -529,6 +529,103 @@ ``` +- to\_timestamp\(string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + Description: Converts a string into a value of the timestamp type according to the format specified by **fmt**. When **fmt** is not specified, perform the conversion according to the format specified by **nls\_timestamp\_format**. + + Returns the value of return_value when a conversion error occurs. + + In **to\_timestamp** in openGauss, + + - If the input year *YYYY* is 0, an error will be reported. + - If the input year *YYYY* is less than 0, specify *SYYYY* in **fmt**. The year with the value of n \(an absolute value\) BC will be output correctly. + + Characters in the **fmt** must match the schema for formatting the data and time. Otherwise, an error is reported. + + Return type: timestamp without time zone + + Example: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + **Table 1** Template patterns for numeric formatting diff --git "a/content/docs-lite/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" "b/content/docs-lite/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" index 6d23c638f..2f9f439b7 100644 --- "a/content/docs-lite/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" +++ "b/content/docs-lite/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" @@ -1161,3 +1161,93 @@ openGauss常用的函数如下: ``` +- to\_timestamp\(string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + 描述:将字符串类型的值转换为指定格式的时间戳。 + + 在转换发生错误时返回 return_value 的值。 + + 返回值类型:timestamp + + 示例: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + \ No newline at end of file diff --git "a/content/docs-lite/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" "b/content/docs-lite/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" index 8ad88553e..0b7454f81 100644 --- "a/content/docs-lite/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" +++ "b/content/docs-lite/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" @@ -562,6 +562,105 @@ ``` +- to\_timestamp\(string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + 描述:将字符串string按fmt指定的格式转换成时间戳类型的值。不指定fmt时,按参数nls\_timestamp\_format所指定的格式转换。 + + 在转换发生错误时返回 return_value 的值。 + + openGauss的to\_timestamp中, + + - 如果输入的年份YYYY=0,系统报错。 + - 如果输入的年份YYYY<0,在fmt中指定SYYYY,则正确输出公元前绝对值n的年份。 + + fmt中出现的字符必须与日期/时间格式化的模式相匹配,否则报错。 + + 返回值类型:timestamp without time zone + + 示例: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + + + **表 1** 数值格式化的模版模式 diff --git a/content/en/docs/BriefTutorial/functions.md b/content/en/docs/BriefTutorial/functions.md index ced9f5622..8b25403b1 100644 --- a/content/en/docs/BriefTutorial/functions.md +++ b/content/en/docs/BriefTutorial/functions.md @@ -1150,3 +1150,96 @@ The common functions of openGauss are as follows: ``` +- to\_timestamp \( text \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + Description: + Converts values of the string type into the timestamp of the specified type. + + DEFAULT return_value ON CONVERSION ERROR is optional. Allows you to specify the value to be returned when a conversion error occurs. + + fmt is optional. Format string. + + Return type: timestamp without time zone + + Example: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + diff --git "a/content/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" "b/content/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" index b49d572f8..315b17d1f 100644 --- "a/content/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" +++ "b/content/zh/docs/BriefTutorial/\345\207\275\346\225\260.md" @@ -1161,3 +1161,94 @@ openGauss常用的函数如下: ``` +- to\_timestamp\(string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + 描述:将字符串类型的值转换为指定格式的时间戳。 + + 在转换发生错误时返回 return_value 的值。 + + 返回值类型:timestamp + + 示例: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + + diff --git "a/content/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" "b/content/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" index e5e973825..7d1e95354 100644 --- "a/content/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" +++ "b/content/zh/docs/SQLReference/\347\261\273\345\236\213\350\275\254\346\215\242\345\207\275\346\225\260.md" @@ -562,6 +562,105 @@ ``` +- to\_timestamp\(string \[ DEFAULT return_value ON CONVERSION ERROR \] \[, fmt\]\) + + 描述:将字符串string按fmt指定的格式转换成时间戳类型的值。不指定fmt时,按参数nls\_timestamp\_format所指定的格式转换。 + + 在转换发生错误时返回 return_value 的值。 + + openGauss的to\_timestamp中, + + - 如果输入的年份YYYY=0,系统报错。 + - 如果输入的年份YYYY<0,在fmt中指定SYYYY,则正确输出公元前绝对值n的年份。 + + fmt中出现的字符必须与日期/时间格式化的模式相匹配,否则报错。 + + 返回值类型:timestamp without time zone + + 示例: + + ``` + openGauss=# SHOW nls_timestamp_format; + nls_timestamp_format + ---------------------------- + DD-Mon-YYYY HH:MI:SS.FF AM + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-2014' DEFAULT '12-sep-20125 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('12-sep-aaa' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + ------------------------- + 2010-09-12 14:10:10.123 + (1 row) + + + openGauss=# SELECT to_timestamp('33-Sep-10 14:10:10.123000' DEFAULT '12-sep-2025 01:00:00' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); + to_timestamp + --------------------- + 2025-09-12 01:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('-1' DEFAULT '-1' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0001-01-01 00:00:00 BC + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '-10' ON CONVERSION ERROR,'SYYYY'); + to_timestamp + ------------------------ + 0010-01-01 00:00:00 BC + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('98' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 1998-01-01 00:00:00 + (1 row) + + openGauss=# SELECT to_timestamp('a' DEFAULT '25' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2025-01-01 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT to_timestamp('01' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2001-01-01 00:00:00 + (1 row) + + + openGauss=# SELECT to_timestamp('a' DEFAULT '11' ON CONVERSION ERROR,'RR'); + to_timestamp + --------------------- + 2011-01-01 00:00:00 + (1 row) + ``` + + + + **表 1** 数值格式化的模版模式 -- Gitee