Solved

Convert date format in DB2 SQL

Posted on 2008-10-24
3
9,368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Dave Ford
Dave Ford 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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