Sql 检查postgres中的值是否为精确格式

Sql 检查postgres中的值是否为精确格式,sql,postgresql,date,casting,postgresql-9.2,Sql,Postgresql,Date,Casting,Postgresql 9.2,如何检查日期值是否为预期格式 我需要检查日期值是否为yyyy-mm-dd格式 如果没有,则显示状态0 dob和doj的数据类型为varchar 表:环境管理计划 eid | ename | dob | doj 1 | abc | 1900-01-19 | 2019-02-20 2 | dx | 1900-29-02 | 2019-04-22 3 | xy | 1989-10-26 | 2018-27-02 4 |ji | 2000-23-01

如何检查日期值是否为预期格式 我需要检查日期值是否为yyyy-mm-dd格式 如果没有,则显示状态0 dob和doj的数据类型为varchar

表:环境管理计划

eid | ename | dob        | doj
1   | abc   | 1900-01-19 | 2019-02-20
2   | dx    | 1900-29-02 | 2019-04-22
3   | xy    | 1989-10-26 | 2018-27-02
4   |ji     | 2000-23-01 | 2019-29-04
表:环境管理计划

eid | ename | dob        | doj
1   | abc   | 1900-01-19 | 2019-02-20
2   | dx    | 1900-29-02 | 2019-04-22
3   | xy    | 1989-10-26 | 2018-27-02
4   |ji     | 2000-23-01 | 2019-29-04
基于以上数据,我希望检查日期格式应为yyyy mm dd,如果不,则标志将显示如下:

eid | dobdateformatstatus | dojdateformatstatus
1   | 1                   | 1
2   | 0                   | 1
3   | 1                   | 0
4   | 0                   | 0

您能告诉我如何编写查询以在PostgreSQL中完成此任务吗。

验证日期不是一项容易的任务,您不想手动执行(您需要考虑每月的可变天数,更不用说闰年了)

不幸的是,Postgres没有
try\u cast()
函数(例如在SQL Server或BigQuery中):失败的强制转换是错误的

一种方法是创建用户函数:

create function try_cast_to_date(p_in text)
returns date 
as $$
begin
    begin
        return $1::date;
    exception 
        when others then return null;
    end;
end;
$$
language plpgsql;
然后,您可以在查询中使用它:

select 
    eid, 
    ename, 
    (try_cast_to_date(dob) is not null) dobdateformatstatus, 
    (try_cast_to_date(doj) is not null) dojdateformatstatus  
from mytable

eid | ename | dobdateformatstatus | dojdateformatstatus --: | :---- | :------------------ | :------------------ 1 | abc | t | t 2 | dx | f | t 3 | xy | t | f 4 | ji | f | f eid | ename | dobdateformatstatus | dojdateformatstatus --: | :---- | :------------------ | :------------------ 1 | abc | t | t 2 | dx | f | t 3 | xy | t | f 4 |吉| f | f
验证日期不是一项容易的任务,您不希望手动执行(您需要考虑每月的可变天数,更不用说闰年了)

不幸的是,Postgres没有
try\u cast()
函数(例如在SQL Server或BigQuery中):失败的强制转换是错误的

一种方法是创建用户函数:

create function try_cast_to_date(p_in text)
returns date 
as $$
begin
    begin
        return $1::date;
    exception 
        when others then return null;
    end;
end;
$$
language plpgsql;
然后,您可以在查询中使用它:

select 
    eid, 
    ename, 
    (try_cast_to_date(dob) is not null) dobdateformatstatus, 
    (try_cast_to_date(doj) is not null) dojdateformatstatus  
from mytable

eid | ename | dobdateformatstatus | dojdateformatstatus --: | :---- | :------------------ | :------------------ 1 | abc | t | t 2 | dx | f | t 3 | xy | t | f 4 | ji | f | f eid | ename | dobdateformatstatus | dojdateformatstatus --: | :---- | :------------------ | :------------------ 1 | abc | t | t 2 | dx | f | t 3 | xy | t | f 4 |吉| f | f
正确的解决方案是将日期值存储在
DATE
列中。然后你就可以确定这是一个有效的日期。在
varchar
列中存储日期(或时间戳或数字)值是一个非常非常糟糕的主意。您是否有机会修复损坏的数据模型?与您的问题无关,但是:Postgres 9.2要求您尽快计划升级。正确的解决方案是将日期值存储在
DATE
列中。然后你就可以确定这是一个有效的日期。在
varchar
列中存储日期(或时间戳或数字)值是一个非常非常糟糕的主意。你有机会修复那个坏掉的数据模型吗?与你的问题无关,但是:Postgres 9.2是您应该尽快计划升级。我们可以不使用函数验证Postgres中的数据吗?如果可以,请告诉我query.don无权创建函数。如果没有函数,则需要完成此任务。我们可以不使用函数验证Postgres中的数据吗?如果可以,请告诉我query.donot有权创建函数。没有函数需要完成此任务。