diff --git a/content/docs-lite/en/docs/BriefTutorial/functions.md b/content/docs-lite/en/docs/BriefTutorial/functions.md index 72f89e71e0986cd12effb90b6755599fbcb96a1b..04f9eea70c305be494747865924d163d409fbf6d 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 b0bfb3b276b7ac2dbe17159718046a5818cb599d..9c6cdbae127ec2c8750a013b2705a1a9300a94a2 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 32240d5911263bb599be370af0a0d49a250ad019..0794f27efbaf0a01ef786b565ac99a905a66035f 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 6d23c638f010f8358e3cbb13c88b50178d89b905..2f9f439b7d6d2d98c92e41f9a553b03a96704f40 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 8ad88553e9df5201ee11b2637d50ea1772841986..0b7454f81d7898f46dfb857855ba9c058c7d40e7 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 ced9f5622b47f9f2e526fba4d6a1a94b24a21b97..8b25403b163edbfb233a67a0f3f79bd79dfaefd9 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 b49d572f81b6947934aa470e7300a84bc6a0a26a..315b17d1fb7f44cb82d53421e5b2df41f436d4fc 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 e5e973825e5dc0e2dfb0b58eb9ad5fe15c343b18..7d1e9535491ef719f6b70bc9c761838ffdaad5df 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** 数值格式化的模版模式