question

Itay B avatar image
Itay B asked

sql datetime field format

Hello, I have a smalldatetime field in my sql table. I would like to show it in a specific format in my queries. As much as i searched, i couldn't find this format in mssql 2005; dd/mm/yyyy hh:mm. this is the exact format i need. anything else is no good. Any idea? Thanks
sql-server-2005sqltsql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Mrs_Fatherjack avatar image
Mrs_Fatherjack answered
This has been answered on another forum here: http://stackoverflow.com/questions/347659/dd-mm-yyyy-hhmm-format-in-sql-server but the answer is: SELECT CONVERT(varchar,GETDATE(),103) + ' ' + SUBSTRING(CONVERT(varchar,GETDATE(),108),1,5) where getdate is replaced by your datetime field.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image WilliamD commented ·
You need to change the first CONVERT() to style 103 instead of 104. This will use slashes between the dateparts instead of dots, otherwise, great! +1 The best overview for date styles can be found [here][1] [1]: http://msdn.microsoft.com/en-us/library/ms187928(v=SQL.90).aspx
0 Likes 0 ·
Mrs_Fatherjack avatar image Mrs_Fatherjack commented ·
Thanks for that, hadn't spotted the dots rather than slashes. Or at least hadn't realised the potential importance.
0 Likes 0 ·
Grant Fritchey avatar image
Grant Fritchey answered
But the most important thing is, don't count on SQL Server for formatting. That's what your front-end code is for. SQL Server should store the information in a useful manner. Your application, report, or spreadsheet can then format it appropriately.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.