Check whether string is a date Postgresql

前端 未结 1 1634
臣服心动
臣服心动 2020-12-10 13:39

Is there any function in PostgreSQL that returns Boolean whether a given string is a date or not just like ISDATE() in MSSQL?

相关标签:
1条回答
  • 2020-12-10 14:13

    You can create a function:

    create or replace function is_date(s varchar) returns boolean as $$
    begin
      perform s::date;
      return true;
    exception when others then
      return false;
    end;
    $$ language plpgsql;
    

    Then, you can use it like this:

    postgres=# select is_date('January 1, 2014');
     is_date
    ---------
     t
    (1 row)
    
    postgres=# select is_date('20140101');
     is_date
    ---------
     t
    (1 row)
    
    postgres=# select is_date('20140199');
     is_date
    ---------
     f
    (1 row)
    
    0 讨论(0)
提交回复
热议问题