Solved

Convert date format in DB2 SQL

Posted on 2008-10-24
3
9,336 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

13 Experts available now in Live!

Get 1:1 Help Now