Solved

Convert date format in DB2 SQL

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select record with the most recent date 14 67
Mysql Left Join Case 10 70
Currency in SQL? 2 30
SQL Syntax 14 34
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

856 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