检查字符串是否为日期 Postgresql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25374707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:35:10  来源:igfitidea点击:

Check whether string is a date Postgresql

sqlpostgresqldate

提问by user3015541

Is there any function in PostgreSQLthat returns Booleanwhether a given string is a date or not just like ISDATE()in MSSQL?

是否有任何函数PostgreSQL返回Boolean给定的字符串是否是日期,就像ISDATE()在 MSSQL 中一样?

ISDATE("January 1, 2014")

回答by ntalbs

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)