雪花:在数字类型列中插入零值

发布于 2025-02-10 16:46:06 字数 565 浏览 1 评论 0原文

我有一个案例语句来纠正雪花中的一种业务逻辑:

INSERT INTO DB.table_b 
    SELECT
        CASE 
            WHEN UPPER(emp) <> LOWER(emp) THEN NULL
            WHEN emp IS NULL THEN nullif(emp, 'NULL')
            ELSE emp  
        END AS emp_no 
    FROM
        DB.table_a;

'table_a'内容如下:

emp
-------
ABCD
NULL
''
23

它包含字符串,空,空和数字。因此,要求仅从案例语句中获取数字和空值,因为'table_b'中的列 emp_no 是数字类型。在源表中,如果列值为字符串,则必须插入 null 值。但是,由于“ table_b'列是类型为'数字'

数字值''未识别

I have a case statement to rectify one business logic in snowflake:

INSERT INTO DB.table_b 
    SELECT
        CASE 
            WHEN UPPER(emp) <> LOWER(emp) THEN NULL
            WHEN emp IS NULL THEN nullif(emp, 'NULL')
            ELSE emp  
        END AS emp_no 
    FROM
        DB.table_a;

The 'table_a' content as below :

emp
-------
ABCD
NULL
''
23

It contains character string, null, empty and numbers. So, the requirement is to take only numbers and empty values from the case statement since the column emp_no in 'table_b' is numeric type. In source table if the column value is string then we have to insert NULL value. But as the 'table_b' column is of type 'numeric' the null value is not getting inserted and getting following error

Numeric value '' is not recognized

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肤浅与狂妄 2025-02-17 16:46:06

使用 try_to_to_to_number

to_decimal,to_number,to_numeric的特殊版本执行相同的操作(即将输入表达式转换为定点号),但是使用错误处理支持(即如果无法执行转换,则返回零值而不是提出错误)。

INSERT INTO DB.table_b 
SELECT TRY_TO_NUMBER(emp) AS emp
FROM DB.table_a;

Using TRY_TO_NUMBER:

A special version of TO_DECIMAL , TO_NUMBER , TO_NUMERIC that performs the same operation (i.e. converts an input expression to a fixed-point number), but with error-handling support (i.e. if the conversion cannot be performed, it returns a NULL value instead of raising an error).

INSERT INTO DB.table_b 
SELECT TRY_TO_NUMBER(emp) AS emp
FROM DB.table_a;
吝吻 2025-02-17 16:46:06

您无法使用is_integer,但对于Varchar(16777216),它不支持

,因此正则表达式会更好

INSERT INTO DB.table_b 
    SELECT
        CASE 
            WHEN regexp_like(emp,'^[0-9]+
) THEN emp
            ELSE NULL  
        END AS emp_no 
    FROM
        DB.table_a;

you can not use IS_INTEGER but for VARCHAR(16777216) it isn't supported

So a regular expression would be better

INSERT INTO DB.table_b 
    SELECT
        CASE 
            WHEN regexp_like(emp,'^[0-9]+
) THEN emp
            ELSE NULL  
        END AS emp_no 
    FROM
        DB.table_a;
花落人断肠 2025-02-17 16:46:06

正如卢卡斯(Lukasz)所述,您应该使用try_to_x functions( try_to_to_numeric 处理解析类型,如果解析失败,则返回null。我要补充的额外说明是,数字/数字和双打将分析0.1234,但得到不同的结果,您没有提及关心,但我认为值得注意,所以我正在添加一个额外的答案来指出区别。

值纳入sql

WITH data(emp) as (
    select * from values
        ('ABCD'),
        (NULL),
        (''),
        ('0.123'),
        ('23')
)
SELECT emp
    ,try_to_numeric(emp) as emp_as_num
    ,try_to_double(emp) as emp_as_float
FROM data
emp_as_numemp_as_as_float'abcd'null23
CTE只是为了nullnull
nullnullnull
nullnullnull
null'0.123'00.123'23'23'23'23'2323
null''null

As Lukasz mentions you should use the TRY_TO_x functions (TRY_TO_NUMERIC, TRY_TO_DOUBLE) as these safely handle parsing the types, and return NULL if the parse fails. The extra note I will add is that both NUMBER/NUMERICs and DOUBLEs will parse 0.1234 but get different results, which you didn't mention as caring about, but I think is worth noting, so I am adding an extra answer to point the difference out.

The CTE is just to get the values into the SQL:

WITH data(emp) as (
    select * from values
        ('ABCD'),
        (NULL),
        (''),
        ('0.123'),
        ('23')
)
SELECT emp
    ,try_to_numeric(emp) as emp_as_num
    ,try_to_double(emp) as emp_as_float
FROM data
EMPEMP_AS_NUMEMP_AS_FLOAT
'ABCD'nullnull
nullnullnull
''nullnull
'0.123'00.123
'23'2323
﹏雨一样淡蓝的深情 2025-02-17 16:46:06

您可以测试AMP为字符串并将字符串设置为空。仅数字值将进入第二个案例语句。

SELECT
        CASE 
            WHEN IS_VARCHAR(emp) then NULL else
                case WHEN UPPER(emp) <> LOWER(emp) THEN NULL ELSE emp end
        end AS emp_no 

You can test for amp being string and set the string to NULL. Only numeric values will go into the second case statement.

SELECT
        CASE 
            WHEN IS_VARCHAR(emp) then NULL else
                case WHEN UPPER(emp) <> LOWER(emp) THEN NULL ELSE emp end
        end AS emp_no 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文