Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date Covertion

Author  Topic 

eem_2055
Yak Posting Veteran

69 Posts

Posted - 2010-01-19 : 05:30:49
Hi To All,

I just want to know how to convert this 05/29/2009 to 20090529?


Best regards,

Edward

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-01-19 : 05:38:52
SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]
Go to Top of Page

eem_2055
Yak Posting Veteran

69 Posts

Posted - 2010-01-19 : 05:41:02
tnx a lot... I got it....
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-01-19 : 05:47:14
welcome
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-19 : 06:04:42
Errmmm ... assuming that "05/29/2009" is in a text string, rather than a DATETIME datatype object, then just using the "112" conversion type will rely on the Locale setting of the server it is run on.

SELECT CONVERT(datetime, '05/29/2009', 101)

will take care of converting a string date (or a column / @variable containing a mm/dd/yyyy date held in string form) into a datetime datatype.

If you then want to convert that to yyyymmdd for output you can either leave it as a DATETIME datatype, and do any formatting in the application (which is preferable), or if you need to do it in SQL then you can use the "112" type, and if you need to do both together that would be:

SELECT CONVERT(char(8), CONVERT(datetime, '05/29/2009', 101), 112) as [YYYYMMDD]

If you've got a string date you could just convert "mm/dd/yyyy" to yyyymmdd" with string functions, like SUBSTRING()
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-25 : 02:13:00
quote:
Originally posted by eem_2055

Hi To All,

I just want to know how to convert this 05/29/2009 to 20090529?


Best regards,

Edward


Why do you want to do this?
If you use front end application, do formation there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -