【发布时间】:2021-05-05 10:50:52
【问题描述】:
【问题讨论】:
标签: date presto trino presto-jdbc
【问题讨论】:
标签: date presto trino presto-jdbc
使用 regexp_like:
select id, start_date
from mytable
where NOT regexp_like(start_date, '\d{4}-\d{2}-\d{2}')
这适用于“11-12-200”和“无”。
如果您还想包含 NULL 值,请添加附加条件:
where (NOT regexp_like(start_date, '\d{4}-\d{2}-\d{2}'))
OR start_date is null
更严格的日期正则表达式是'^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$'
这将限制月份为01 .. 12 和日期为01 .. 31 并且不允许在日期之前和之后的其他字符(使用^ 和$ 锚点)。
一种更简单、更强大的方法
是使用try_cast(col as date) - 如果无法转换,它将返回 NULL:
where try_cast(start_date as date) is not null
这也将限制错误日期,例如 2 月 30 日 (2000-02-30)
【讨论】: