【问题标题】:DB2 SQL capturing errors on date formatingDB2 SQL 捕获数据格式化错误
【发布时间】:2020-04-05 02:02:09
【问题描述】:

如果我在 DB2 SQL 日期转换中遇到错误,我可以使用什么语法来捕获/报告 错误,同时允许作业继续。例如:写入文件或记录或打印文件?

示例: TIMESTAMP_FORMAT(CHAR(MYDATE)) 其中 MYDATE = '20200132'

错误信息:

 Message . . . . :   Expression not valid using format string specified      
   for TIMESTAMP_FORMAT.                                                     
 Cause . . . . . :   Argument 1 of the TIMESTAMP_FORMAT function can not     
   be interpreted using the format string specified in argument 2 for        
   one of the following reasons:                                             
    -- The string expression is too short or too long.                       
    -- The string expression does not conform to the template specified      
       in the format string.                                                 
    -- Too many digits were specified in the string expression for the       
       corresponding format element in the format string.                    
    -- A value in the string expression is not valid for the                 
       corresponding format element in the format string.                    
 Recovery  . . . :   Specify a valid format string for the function. Try        

【问题讨论】:

标签: sql db2-400


【解决方案1】:

您可以使用用户定义函数 (UDF) 来处理转换并捕获错误。

在无效时返回 NULL,或者可能为 9999999-->9999-12-31。

您可以构建或拥有或查找一些示例代码。特别是对于 IBM i 的 Db2,有一个由 Alan Campin 命名的名为 iDate 的开源库,被许多人使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多