Solved

Convert date format in DB2 SQL

Posted on 2008-10-24
3
9,347 Views
Last Modified: 2008-10-25
Is there an existing function in DB2 SQL, to convert date format from '10/24/2008' to '2008-10-24'
AND
is there an equivalent to DATEPART('yyyy', MyDate) in DB2 SQL
Thanks
0
Comment
Question by:Yossele
3 Comments
 
LVL 5

Assisted Solution

by:jfmador
jfmador earned 25 total points
ID: 22798629
Hello

Under DB2 you can try Year(myDate)

Here is a redbook from ibm about conversion between MSSQL query and db2
http://www.redbooks.ibm.com/redbooks/pdfs/sg246672.pdf
0
 
LVL 45

Accepted Solution

by:
Kdo earned 50 total points
ID: 22798634
Hi Yossele,

Internally, DB2 stores a date as an integer.  (Oddly, October 24, 2008 is stored as the integer value 20081024 and not as a day count.)  The concept of a date format is purely a display option.

You can set the default date format to be most any of the widely accepted formats.  You can even customize it to meet your needs.

As for extracting part of a date, see the YEAR, MONTH, and DAY functions.

SELECT * FROM sometable WHERE year (rowdate) = 2008;


Good Luck,
Kent
0
 
LVL 18

Assisted Solution

by:daveslash
daveslash earned 50 total points
ID: 22798694

It kind of depends on how the date is stored in your database.

Ideally, the column would have a datatype of DATE. If that's the case, then setting the date-format in your connection-settings is pretty easy. (odbc ... jdbc ... etc)

Unfortunately, I've often seen dates stored as 10-character strings. Translating that to a date is pretty easy, though, by just using the DATE function. Then, as long as your date-format is set correctly, it'll return in your desired format.

(see example below)

HTH,
DaveSlash

With a varchar date:
 

select vDate,

       char(date(vDate),iso)

from   dford/deleteme
 

VDATE       CHAR conversion

10/24/2008    2008-10-24   

10/17/2008    2008-10-17   

09/24/2008    2008-09-24   

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now