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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 date format yyyymmdd

Author  Topic 

Ned
Starting Member

16 Posts

Posted - 2004-08-31 : 13:47:20
Hi,
I need to return a date string in the following format: yyyymmdd. What is the best way to do this? This is close, but I don't see how to 0 pad the numbers:

SELECT
CAST(DATEPART(yyyy,GETDATE()) AS Varchar) +
CAST(DATEPART(mm,GETDATE()) AS Varchar) +
CAST(DATEPART(dd,GETDATE()) AS Varchar)

the above returns 2004831. I would like 20040831. I have tried a number of the date functions with no luck.

Thanks

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-31 : 13:49:30
This should do it... although there might be a predefined format in convert(nvarchar,getdate(),???)

SELECT
CAST(DATEPART(yyyy,GETDATE()) AS Varchar) +
Right('00' + CAST(DATEPART(mm,GETDATE()) AS Varchar),2) +
Right('00' + CAST(DATEPART(dd,GETDATE()) AS Varchar),2)

Corey
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-31 : 13:53:21
this should do the trick:

select convert(varchar(10), GETDATE(), 112)

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-31 : 14:00:57
good... i was too lazy to look it up

Corey
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-31 : 14:02:21
yeah... lets do it iso style :)

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-08-31 : 14:21:09
date formatting should be done at the presentation layer.

- Jeff
Go to Top of Page
   

- Advertisement -