ISDATE 相当于 DB2

发布于 2024-12-06 09:53:35 字数 111 浏览 3 评论 0原文

我有一个包含字符格式日期的表。我想检查日期的格式。请让我知道如何在 DB2 中做到这一点。我知道有一个函数 ISDATE 但它在 DB2 中不起作用。我在 AS400 上使用 db2 作为日期基础..请帮助我

I have a table which contains date in character format . I want to check the format of date . Please let me know how i can do this in DB2. I know there is a function ISDATE but its not working in DB2. I am on AS400 using db2 as date base .. Please help me out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-12-13 09:53:35

实际上,看起来 AS/400 的 DB2 可能没有ISDATE() 函数(我在 V6R1 参考中找不到任何内容 - 或者,有趣的是, LUW 参考)。所以你的问题似乎是该功能不存在。

当然,问题的根源在于尝试转换无效日期会导致语句停止。鉴于此,如果可以进行格式化,则此语句应该为您提供一个日期,如果不可能,则为 null。请注意,如果您混合了美国和欧元格式/排序,您可能无法正确恢复数据(如果您的分隔符不同,我认为它们默认情况下,你可能会没事的)。

WITH date_format(strDate, format) as (
    SELECT strDate, 
        CASE 
            WHEN strDate LIKE('____-__-__') THEN 'ISO'
            WHEN strDate LIKE('__.__.____') THEN 'EUR'
            WHEN strDate LIKE('__/__/____') THEN 'USA'
            ELSE NULL END
    FROM dataTable
)
SELECT
    strDate, 
    format, 
    CASE 
        WHEN format IS NOT NULL THEN DATE(strDate)
        ELSE NULL 
    END AS realDate
FROM date_format

这会将数据表变成这样:

String Dates
=============
2011-09-22   
22.09.2011   
09/22/2011   
a111x90x00  -- And who knows what this is...  

变成这样:

Results:
strDate      format   realDate
============================
2011-09-22   ISO      2011-09-22   
22.09.2011   EUR      2011-09-22   
09/22/2011   USA      2011-09-22   
a111x90x00   -        -            

这个示例当然使用自动翻译的默认格式。如果您有其他内容,则必须手动翻译它(您可以将其子串到 ISO 然后进行转换,而不是返回格式)。

Actually, it looks like DB2 for the AS/400 may not have the ISDATE() function (I can't find anything in the V6R1 reference - or, interestingly, the LUW reference either). So your problem appears to be that the function does not exist.

The root of the problem, of course, is that attempting to translate an invalid date causes the statement to halt. In light of that, this statement should give you a date if the formatting was possible, and null if it was not. Please note that if you've mixed USA and EUR formats/ordering, you might not be able to correctly recover the data (if your separators are different, which I think they are by default, you'll probably be okay).

WITH date_format(strDate, format) as (
    SELECT strDate, 
        CASE 
            WHEN strDate LIKE('____-__-__') THEN 'ISO'
            WHEN strDate LIKE('__.__.____') THEN 'EUR'
            WHEN strDate LIKE('__/__/____') THEN 'USA'
            ELSE NULL END
    FROM dataTable
)
SELECT
    strDate, 
    format, 
    CASE 
        WHEN format IS NOT NULL THEN DATE(strDate)
        ELSE NULL 
    END AS realDate
FROM date_format

This turns a dataTable looking like this:

String Dates
=============
2011-09-22   
22.09.2011   
09/22/2011   
a111x90x00  -- And who knows what this is...  

Into this:

Results:
strDate      format   realDate
============================
2011-09-22   ISO      2011-09-22   
22.09.2011   EUR      2011-09-22   
09/22/2011   USA      2011-09-22   
a111x90x00   -        -            

This example is of course using the default formats which auto-translate. If you have something else, you'll have to manually translate it (instead of returning the format, you can substring it into ISO then cast it).

烂人 2024-12-13 09:53:35

我不清楚“检查日期的格式”的意图,也不知道现有的 ISDATE() 效果是什么,但名称所暗示的内容似乎足够清楚。考虑:

在 v5r3 上进行了极其简单的测试 [即只是为了确保一些错误的日期字符串示例值和一些垃圾文本输入实际上返回 NULL,并且每个标准 具有有效值的日期格式返回日期值],以下内容应影响输入变化字符串的评估,最多 10 个字符作为可转换为 DATE 的值;当输入值无法转换为日期时,结果为 NULL。当然,这意味着最初的 NULL 值不能直接与无效日期字符串生成的结果区分开来。

如果需要某些所需的指示符,例如 TRUEFALSE 作为结果,则可以在 CASE 表达式中编码使用用户定义函数 (UDF);例如:
当 ISDATE(myVCcol) 为 NULL 时,则为 'FALSE' ELSE 'TRUE' END

DROP   FUNCTION ISDATE                              
;                                                   
CREATE FUNCTION ISDATE                              
 ( InpDateStr VARCHAR( 10 )                         
 ) RETURNS DATE                                     
 LANGUAGE SQL                                       
 DETERMINISTIC                                      
 RETURNS NULL ON NULL INPUT                         
    SET OPTION DBGVIEW = *SOURCE , DATFMT = *ISO    
BEGIN                                               
 DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN NULL ;
 RETURN DATE( InpDateStr ) ;                        
END                                                 
;                                                   

I am not clear on what "check the format of date" intends, nor do I know what any existing ISDATE() effects, but what is implied by the name seems clear enough. Consider:

Extremely lightly tested on v5r3 [i.e. only to ensure both that a few bad date character string example values and some garbage text input actually returned NULL and that each of the various standard date formats with valid values returned a date value], that the following should effect the evaluation of the input varying character string up to 10 characters as a value that can be cast to DATE; that when the input value can not be cast to date, then the result is NULL. Of course that means an initially NULL value is not directly distinguishable from the result produced for an invalid date string.

If some desired indicator, such as TRUE or FALSE is desired as the result instead, then the use of the User Defined Function (UDF) could be coded in a CASE expression; e.g.:
CASE WHEN ISDATE(myVCcol) IS NULL THEN 'FALSE' ELSE 'TRUE' END

DROP   FUNCTION ISDATE                              
;                                                   
CREATE FUNCTION ISDATE                              
 ( InpDateStr VARCHAR( 10 )                         
 ) RETURNS DATE                                     
 LANGUAGE SQL                                       
 DETERMINISTIC                                      
 RETURNS NULL ON NULL INPUT                         
    SET OPTION DBGVIEW = *SOURCE , DATFMT = *ISO    
BEGIN                                               
 DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN NULL ;
 RETURN DATE( InpDateStr ) ;                        
END                                                 
;                                                   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文