views:

51

answers:

3

I want to SELECT a formatted date string from a datetime type in SQL Server 2005.

In the format "yyyy/mm/dd hh:mm:ss".

What is the best way to do using only a query?

+6  A: 

Check out the CONVERT statement.

SELECT CONVERT(VARCHAR(20), getdate(), 120)

is closest to what you want. (Note the different separators (- instead of / ))

Frederik Gheysels
+2  A: 

this link has what all the different convert styles are.

DForck42
+3  A: 
 select convert(varchar, datetime_field, 120) from tablename;

will do almost what you want.

120 is the conversion "style", see here for more.

tpdi