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

时间:2019-02-28 01:19:19

标签: sql text numeric snowflake-datawarehouse

我有一列同时包含数字和文本数据。因为数字代表一个组,文本代表另一组。但是,该列在SQL中存储为文本。无论如何,我是否可以基于此列将它们分为两组?顺便说一句,我正在使用雪花数据仓库。

enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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