Presto SQL 在 where 子句中按数据类型过滤
Presto SQL filter by data type in where clause
我有一列,应该是VARCHAR;我需要将这些作为双打。但是,某些值是布尔值并会触发错误 Cannot cast false to DOUBLE
。我该如何防止这是 WHERE 原因?
快速完成以下操作的最简单方法是什么?
...
WHERE Type(col) != BOOL
或者
...
WHERE type(col) = VARCHAR
您可以使用 try_cast
并过滤掉 null
s:
-- sample data1
WITH dataset (column) AS (
VALUES ('1'),
('not a double')
)
--query
select *
from (
select try_cast(column as double) as column
from dataset
)
where column is not null
输出:
column
1.0
或在where
(where try_cast(...) is not null
)
中使用
我有一列,应该是VARCHAR;我需要将这些作为双打。但是,某些值是布尔值并会触发错误 Cannot cast false to DOUBLE
。我该如何防止这是 WHERE 原因?
快速完成以下操作的最简单方法是什么?
...
WHERE Type(col) != BOOL
或者
...
WHERE type(col) = VARCHAR
您可以使用 try_cast
并过滤掉 null
s:
-- sample data1
WITH dataset (column) AS (
VALUES ('1'),
('not a double')
)
--query
select *
from (
select try_cast(column as double) as column
from dataset
)
where column is not null
输出:
column |
---|
1.0 |
或在where
(where try_cast(...) is not null
)