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 2008 Forums
 Transact-SQL (2008)
 Need a specific date format

Author  Topic 

Goalie35
Yak Posting Veteran

81 Posts

Posted - 2011-09-20 : 14:01:07
Lets say I have a date in my db like the following:
2011-09-20 11:29:00

I need to set the format as YYYYMMDD HH:MM:SS. So the above date would look like the following:
20110920 11:29:00

How can I do this format? I see I can use CONVERT(VARCHAR(8), GETDATE(), 112) to get "20110920", or CONVERT(VARCHAR(8), GETDATE(), 108) to get "11:29:00", but I don't see a way to get both in one shot.

Worst case, I could do something like this:
CONVERT(VARCHAR(8), GETDATE(), 112) + ' ' + CONVERT(VARCHAR(8), GETDATE(), 108)

...but I was curious if there was a simpler (cleaner) way.
Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-20 : 22:11:59
why should you be concerned on format in which dates are stored in db? So far as datatype is datetime you can retrieve and show it in any format you want using front end functions. In db, all date values are internally stored as integer and when selected they will be shown in standard ISO format YYYY-MM-DD HH:MM:SS . So no need to change the way they're stored. For changing the format in T-sql you need to use convert and make it varchar but doing it in table should be highly avoided as it results in lots f unwanted casts while working with date values and also makes date manipulations difficult.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -