我有一个名为"start_ date";如下所示。start_ date的数据类型是";文本";。
start_date
2019-02-28
2020-01-30
31-02-2019
如果日期格式是"0";yyyy-mm-dd";然后我想将其标记为"0"。否则,我们需要将其标记为1。我们如何实现这个用例语句。
预期输出
start_date Flag
2019-02-28 0
2020-01-30 0
31-02-2019 1
嗯。这可能足够接近您的目的:
select (not (start_date ~ ^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$')) as flag
这将检查格式是否为DDDD-DD-DD。它不会验证日期是否实际有效。
如果您需要更详细的表达式,请尝试:
'^((0[0-9]{2}[1-9]|[1-9][0-9]{3})-((0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-8])|(0[469]|11)-(0[1-9]|[12][0-9]|30))$)|^(dd(0[48]|[2468][048]|[13579][26])-02-29$)'
最好将其隐藏在函数后面。以下函数评估字符串是否包含ISO格式的日期。除非参数为null,否则返回true,否则返回false。请参阅演示。
create or replace
function is_valid_iso_date (iso_date_string text)
returns boolean
language sql
immutable strict
/*
regexp to determine if a string is validly formatted as an ISO_DATE. Format 'yyyy-mm-dd'.
The following validates a string as being a valid ISO date (or not) for dates range
between 0001-01-01 and 9999-12-31 including leap days (Feb-29).
It is however based on a faulty assumption: That the Georgian Calendar has been official and
consistent 0001-01-01 but this is not true. The Georgian Calendar was introduced in OCT 1582.
*/
as $$
select iso_date_string ~ '^((0[0-9]{2}[1-9]|[1-9][0-9]{3})-((0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-8])|(0[469]|11)-(0[1-9]|[12][0-9]|30))$)|^(dd(0[48]|[2468][048]|[13579][26])-02-29$)'
$$;