Jobs

Convert to date

Convert to date

Convert to date

I am using version db2 9.1 I need to convert numeric field that holds dates defined as DECIMAL (8,0) in YYYYMMDD format. I would like to convert it to MM/dd/YYYY.I would perferred to remove leading zeros in the month and day part. for example 7/3/2015 not 07/03/2015. any tips will be appreciated. Thank you in advance. This is really important many thanks in advance.

RE: Convert to date

I am not in work so can't try this at the moment, but I would've thought you would need a combination of the following.
Use Char, Digits & Strip/Ltrim to get a string version without leading zeroes, then use Concat, Substr & Strip/Ltrim to create the date you want (I can't remember when STRIP was introduced).

e.g.
Something like this to get the date as YYYYMMDD in character format STRIP(CHAR(DIGITS(dec_date)),'0')
Then STRIP(SUBSTR(STRIP(CHAR(DIGITS(dec_date)),'0'),7,2),'0') should give the DD portion without leading zeroes.
You can then concatenate the different portions of the substring date together

I am by no means an expert, so there may be a better way.
As I said, I haven't been able to run the above, so this may need some tweaking but will hopefully give you enough ideas to work it out.

explanation
A - digits - convert input data to a char format so following it is valid for the next function
B - timestamp_format - convert A into a valid date

Note that the above is all that is required to convert the field from a number to a date
the following are steps only needed for a visual representation of the date.

C - varchar_format - convert B into a valid string representation of a date on format mm/dd/yyyy
D - replace - searches string represented by C and replaces the inner occurence of /0 by a /
E - strip - removes the leading zero of the resulting value of D