Solved

Convert date format in DB2 SQL

Posted on 2008-10-24
3
9,363 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:
Kent Olsen 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 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