Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert to date 1

Status
Not open for further replies.
Sep 22, 2013
18
0
0
US
Hello,

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.
 
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.
 
from the manuals - unfortunately the one function that should allow you to do it does not seem to have the option to remove leading zeros from day/month part


It is for systems other than mainframe - note that mainframe version no always have the same functionality. sometimes they have more others they have less.

strip(replace(VARCHAR_FORMAT(TIMESTAMP_FORMAT(DIGITS(numeric_date_field), 'YYYYMMDD'),'MM/DD/YYYY'),'/0','/'),LEADING,'0')

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



Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
bibulous, fredericofonseca

Thanks for the tip I appreciate your insight, but fredericofonseca what is the function even if it doesn't remove leading zeros.

How would I code that as a sql statement

for example I am using

Select DateShip, ShipAmt
From ShippedOrders

Again thanks for your help.

 
Select strip(replace(VARCHAR_FORMAT(TIMESTAMP_FORMAT(DIGITS(DateShip), 'YYYYMMDD'),'MM/DD/YYYY'),'/0','/'),LEADING,'0')
, ShipAmt
From ShippedOrders


Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top