INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Convert to date

Convert to date

(OP)
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.

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.

RE: Convert to date

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

http://public.dhe.ibm.com/ps/products/db2/info/vr9...

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
www.syssoft-int.com

FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: Convert to date

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close