检查字符串是否为有效日期

2021-12-24 00:00:00 oracle plsql

我有一个带有 sourcefilename 字段的表,它有 5 条记录.以下是记录.

I have a table with field sourcefilename which has 5 records. Following are the records.

SN. SOURCEFILENAME
1. 20170215095453_1.Iredell Memorial Hospital Dental Eligibility.xls_INFREPT01.txt
2. Iredell Memorial Hospital Eligibility April 2017.xls_INFREPT01.txt
3. Iredell Memorial Hospital Eligibility March 2017.xls_INFREPT01.txt
4. Iredell Memorial Hospital Eligibility May 2017.xls_INFREPT01.txt
5. Iredell Memorial Hospital October 2016 Dental Eligibility.xls_EligData.txt

我只需要提取前 8 个字符并检查其是否为有效日期.如果它是一个有效日期,则返回 TRUE,否则返回 FALSE.

I just need to extract first 8 characters and check if its a valid date. If it is a valid date then return TRUE else FALSE.

我尝试过 ISDATE 函数.还有其他选择吗?

I tried ISDATE function. Is there are any other alternatives?

SELECT DISTINCT SubStr(sourcefilename,1,8),
CASE WHEN isdate(SubStr(sourcefilename,1,8),'YYYYMMDD') = 1 THEN 'TRUE' ELSE 'FALSE' END  FROM ai_4451_1_metl;

推荐答案

Oracle 12.2 提供了一个新功能 VALIDATE_CONVERSION.到目前为止,我从未使用过它,但我认为它会是这样的:

Oracle 12.2 provides a new function VALIDATE_CONVERSION. So far I never used it, but I assume it would be like this:

CASE VALIDATE_CONVERSION(SubStr(sourcefilename,1,8) AS DATE, 'YYYYMMDD') 
   WHEN 1 THEN 'TRUE'
   ELSE 'FALSE'
END

相关文章