From 2cb22efcae1537ed5150d56ff407a08c3d1fe97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=97=E9=87=8C=E5=AF=BB=E4=BB=96=E5=8D=83=E7=99=BE?= =?UTF-8?q?=E5=BA=A6?= <783479891@qq.com> Date: Wed, 19 Mar 2025 15:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0cast=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\207\275\346\225\260.md" | 79 ++++++++++++++++++ ...54\346\215\242\345\207\275\346\225\260.md" | 80 +++++++++++++++++++ .../\345\207\275\346\225\260.md" | 79 ++++++++++++++++++ ...54\346\215\242\345\207\275\346\225\260.md" | 66 +++++++++++++++ 4 files changed, 304 insertions(+) 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 4aa1ccf68..eab93e42d 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" @@ -1003,6 +1003,85 @@ openGauss常用的函数如下: (1 row) ``` +- cast\(x as y\) + + 描述:类型转换函数,将x转换成y指定的类型。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('22-oct-1997' as timestamp); + timestamp + --------------------- + 1997-10-22 00:00:00 + (1 row) + ``` + +- CAST( expr AS type_name [ DEFAULT return_value ON CONVERSION ERROR ][, fmt [, 'nlsparam' ] ]) + + 描述:类型转换函数,将x转换成y指定的类型,并且支持DEFAULT设置默认值、fmt指定入参的输入格式以及nlsparam参数,目前nlsparam参数仅支持English和American。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('12,454.8-' as numeric, '99G999D9S'); + numeric + ---------- + -12454.8 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('111111.111'+'1111' as numeric, '99G999D9S'); + numeric + ------------ + 112222.111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('1e5'+'1111' as numeric, '999999.99'); + numeric + --------- + 101111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('此参数错误' as numeric default 321456231 on conversion error, '999,999,999,999.99'); + numeric + ----------- + 321456231 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('12-sep-2014' as timestamp , 'DD-Mon-YYYY'); + timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('此为错误输入' as timestamp DEFAULT '11-01-11 14:10:10.123000' ON CONVERSION ERROR,'DD-MM-RR HH24:MI:SS.FF'); + timestamp + ------------------------- + 2011-11-01 14:10:10.123 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('01-Jan-03 14:10:10.123000' as timestamp DEFAULT '11-Jan-11 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-RR HH24:MI:SS.FF','NLS_DATE_LANGUAGE = American'); + timestamp + ------------------------- + 2003-01-01 14:10:10.123 + (1 row) + ``` - to\_hex\(number int or bigint\) 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 3462c8614..0445bf719 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" @@ -144,6 +144,86 @@ (1 row) ``` +- cast\(x as y\) + + 描述:类型转换函数,将x转换成y指定的类型。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('22-oct-1997' as timestamp); + timestamp + --------------------- + 1997-10-22 00:00:00 + (1 row) + ``` + +- CAST( expr AS type_name [ DEFAULT return_value ON CONVERSION ERROR ][, fmt [, 'nlsparam' ] ]) + + 描述:类型转换函数,将x转换成y指定的类型,并且支持DEFAULT设置默认值、fmt指定入参的输入格式以及nlsparam参数,目前nlsparam参数仅支持English和American。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('12,454.8-' as numeric, '99G999D9S'); + numeric + ---------- + -12454.8 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('111111.111'+'1111' as numeric, '99G999D9S'); + numeric + ------------ + 112222.111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('1e5'+'1111' as numeric, '999999.99'); + numeric + --------- + 101111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('此参数错误' as numeric default 321456231 on conversion error, '999,999,999,999.99'); + numeric + ----------- + 321456231 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('12-sep-2014' as timestamp , 'DD-Mon-YYYY'); + timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('此为错误输入' as timestamp DEFAULT '11-01-11 14:10:10.123000' ON CONVERSION ERROR,'DD-MM-RR HH24:MI:SS.FF'); + timestamp + ------------------------- + 2011-11-01 14:10:10.123 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('01-Jan-03 14:10:10.123000' as timestamp DEFAULT '11-Jan-11 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-RR HH24:MI:SS.FF','NLS_DATE_LANGUAGE = American'); + timestamp + ------------------------- + 2003-01-01 14:10:10.123 + (1 row) + ``` + - to\_char\(datetime/interval \[, fmt\]\) 描述:将一个DATE、TIMESTAMP、TIMESTAMP WITH TIME ZONE或者TIMESTAMP WITH LOCAL TIME ZONE类型的DATETIME或者INTERVAL值按照fmt指定的格式转换为TEXT类型。 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 7c03d5653..5b9bdae4d 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" @@ -1002,6 +1002,85 @@ openGauss常用的函数如下: (1 row) ``` +- cast\(x as y\) + + 描述:类型转换函数,将x转换成y指定的类型。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('22-oct-1997' as timestamp); + timestamp + --------------------- + 1997-10-22 00:00:00 + (1 row) + ``` + +- CAST( expr AS type_name [ DEFAULT return_value ON CONVERSION ERROR ][, fmt [, 'nlsparam' ] ]) + + 描述:类型转换函数,将x转换成y指定的类型,并且支持DEFAULT设置默认值、fmt指定入参的输入格式以及nlsparam参数,目前nlsparam参数仅支持English和American。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('12,454.8-' as numeric, '99G999D9S'); + numeric + ---------- + -12454.8 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('111111.111'+'1111' as numeric, '99G999D9S'); + numeric + ------------ + 112222.111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('1e5'+'1111' as numeric, '999999.99'); + numeric + --------- + 101111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('此参数错误' as numeric default 321456231 on conversion error, '999,999,999,999.99'); + numeric + ----------- + 321456231 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('12-sep-2014' as timestamp , 'DD-Mon-YYYY'); + timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('此为错误输入' as timestamp DEFAULT '11-01-11 14:10:10.123000' ON CONVERSION ERROR,'DD-MM-RR HH24:MI:SS.FF'); + timestamp + ------------------------- + 2011-11-01 14:10:10.123 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('01-Jan-03 14:10:10.123000' as timestamp DEFAULT '11-Jan-11 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-RR HH24:MI:SS.FF','NLS_DATE_LANGUAGE = American'); + timestamp + ------------------------- + 2003-01-01 14:10:10.123 + (1 row) + ``` - to\_hex\(number int or bigint\) 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 03864edad..3267a2981 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" @@ -19,6 +19,8 @@ - cast\(x as y\) 描述:类型转换函数,将x转换成y指定的类型。 + + 返回值类型:type_name 示例: @@ -30,6 +32,70 @@ (1 row) ``` +- CAST( expr AS type_name [ DEFAULT return_value ON CONVERSION ERROR ][, fmt [, 'nlsparam' ] ]) + + 描述:类型转换函数,将x转换成y指定的类型,并且支持DEFAULT设置默认值、fmt指定入参的输入格式以及nlsparam参数,目前nlsparam参数仅支持English和American。 + + 返回值类型:type_name + + 示例: + + ``` + openGauss=# SELECT cast('12,454.8-' as numeric, '99G999D9S'); + numeric + ---------- + -12454.8 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('111111.111'+'1111' as numeric, '99G999D9S'); + numeric + ------------ + 112222.111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('1e5'+'1111' as numeric, '999999.99'); + numeric + --------- + 101111 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('此参数错误' as numeric default 321456231 on conversion error, '999,999,999,999.99'); + numeric + ----------- + 321456231 + (1 row) + ``` + + ``` + openGauss=# SELECT cast('12-sep-2014' as timestamp , 'DD-Mon-YYYY'); + timestamp + --------------------- + 2014-09-12 00:00:00 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('此为错误输入' as timestamp DEFAULT '11-01-11 14:10:10.123000' ON CONVERSION ERROR,'DD-MM-RR HH24:MI:SS.FF'); + timestamp + ------------------------- + 2011-11-01 14:10:10.123 + (1 row) + ``` + + ``` + openGauss=# SELECT cast ('01-Jan-03 14:10:10.123000' as timestamp DEFAULT '11-Jan-11 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-RR HH24:MI:SS.FF','NLS_DATE_LANGUAGE = American'); + timestamp + ------------------------- + 2003-01-01 14:10:10.123 + (1 row) + ``` + - hextoraw\(raw\) 描述:将一个十六进制构成的字符串转换为raw类型。 -- Gitee