当整列存储为文本时,如何在SQL中分隔数字和文本?

问题描述 投票:1回答:2

我有一个包含数字和文本数据的列。因为数字代表一个组,而文本代表另一个组。但是,该列在SQL中以文本形式存储。无论如何我可以根据这个专栏将它们分成两组吗?顺便说一下,我正在使用雪花数据仓库。

sql text numeric snowflake-datawarehouse
2个回答
0
投票

您可以使用正则表达式。例如,要将数值放在第一位:

select t.*
from t
order by (case when column1 rlike '^[0-9]+$' then 1 else 2 end)

0
投票

最简单的方法是使用TRY_TO_NUMERIC(),如果它是可转换的,则返回数值,否则返回NULL。所以,例如:

create table xx(s string);
insert into xx values('hello_there'), ('1234'), ('look_out'), ('452');

select s, -- original column
       try_to_numeric(s) snum,  -- numeric version of the column
       case when snum IS NULL then s else NULL end sstr -- non-numeric string
from xx;

Here are the results

© www.soinside.com 2019 - 2024. All rights reserved.