简体   繁体   English

oracle中是否有isdate()等价的函数

[英]Is there an equivalent function to isdate () in oracle

CASE WHEN ISDATE(LTRIM(RTRIM(rard.thevalue))) = 1     
THEN CONVERT(smalldatetime, LTRIM(RTRIM(rard.thevalue)))
WHEN ISDATE(LTRIM(RTRIM(rard2.thevalue))) = 1
THEN CONVERT(smalldatetime, LTRIM(RTRIM(rard2.thevalue)))
ELSE CONVERT(smalldatetime, LTRIM(RTRIM(r.receiptdate)))

I have this syntax in SQL which has to get converted into oracle.我在 SQL 中有这种语法,它必须转换为 oracle。 The column "thevalue" has different formats in it ex: HH:MM , MM/DD/YYYY, HH:MM:SS etc. So isdate() function is checking whether its matching the date format and then pulling the data. “thevalue”列有不同的格式,例如:HH:MM、MM/DD/YYYY、HH:MM:SS 等。所以 isdate() 函数正在检查它是否与日期格式匹配,然后提取数据。 I would need similar kind of function to check whether the columns value is matching date time format and then display as date.我需要类似的函数来检查列值是否匹配日期时间格式,然后显示为日期。

The Oracle equivalent would be validate_conversion() . Oracle 等效项是validate_conversion()

However, unlike SQL Server, Oracle won't recognize varying formats.但是,与 SQL Server 不同的是,Oracle 不会识别不同的格式。 You need to explicitly specify the format that you want (unless your dates already are in the format configured by nls_date_format ).您需要明确指定所需的格式(除非您的日期已经采用nls_date_format配置的格式)。 Basically, you could test each possible format one after the other, and stop whenever one is recognized.基本上,您可以一个接一个地测试每种可能的格式,并在识别出一种时停止。

Since your purpose is to actually convert the string to a date , it would be simpler to use directly to_date() , with the on conversion error clause.由于您的目的是将字符串实际转换为date ,直接使用to_date()on conversion error子句会更简单。

Consider something like:考虑这样的事情:

coalesce(
    to_date(thevalue default null on conversion error, 'MM/DD/YYYY'),
    to_date(thevalue default null on conversion error, 'YYYY-MM-DD HH24:MI:SS'),
    ...
) 

Notes:笔记:

  • the function happily ignores leading and trailing spaces, so there is no need to trim() beforehand该函数愉快地忽略了前导和尾随空格,因此无需事先trim()

  • this requires Oracle 12.2 or higher这需要 Oracle 12.2 或更高版本

  • isdate() is not really safe in SQL Server; isdate()在 SQL Server 中并不安全; better use try_convert() , which basically behaves like Oracle's to_date() with default null on conversion error更好地使用try_convert() ,它的行为基本上类似于 Oracle 的to_date() default null on conversion error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM