简体   繁体   中英

DB2 SQL capturing errors on date formating

If I get an error on a DB2 SQL date conversion, what syntax can I use to capture/report on the error, while allowing job to continue. eg: write to file or log or print file?

example: TIMESTAMP_FORMAT(CHAR(MYDATE)) Where MYDATE = '20200132'

error message:

 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        

You can use a User Defined Function (UDF) to handle the conversion and catch errors.

returning NULL when invalid, or perhaps for 9999999-->9999-12-31.

You can build or own or find some example code. For Db2 for IBM i specifically, there's an open source library named iDate by Alan Campin used by many.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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