雪花:在数字类型列中插入零值
我有一个案例语句来纠正雪花中的一种业务逻辑:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 try_to_to_to_number
Using TRY_TO_NUMBER:
您无法使用
is_integer
,但对于Varchar(16777216),它不支持,因此正则表达式会更好
you can not use
IS_INTEGER
but for VARCHAR(16777216) it isn't supportedSo a regular expression would be better
正如卢卡斯(Lukasz)所述,您应该使用
try_to_x
functions( try_to_to_numeric ,处理解析类型,如果解析失败,则返回null。我要补充的额外说明是,数字/数字和双打将分析0.1234
,但得到不同的结果,您没有提及关心,但我认为值得注意,所以我正在添加一个额外的答案来指出区别。值纳入sql
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 parse0.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:
您可以测试AMP为字符串并将字符串设置为空。仅数字值将进入第二个案例语句。
You can test for amp being string and set the string to NULL. Only numeric values will go into the second case statement.