October 09, 2010

Display Dates in a Particular Format in SQL Server

Questions of displaying dates in either ‘mm/dd/yy’ format or ‘dd/mm/yyyy’ format and so on have been asked numerous times. A very good link to check out the different Date and Time styles is the CAST and CONVERT functions in BOL

Here’s an example of displaying dates in ‘dd/mm/yyyy’ format

ID int,
DateOfEntry DateTime

SELECT 1, '08/22/2010' UNION ALL
SELECT 2, '09/01/2010' UNION ALL
SELECT 3, '10/03/2010' UNION ALL
SELECT 4, '10/07/2010'

SELECT ID, CONVERT(VARCHAR(10),DateOfEntry,103) AS 'dd/mm/yyyy'



About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

1 comment:

Joel said...

Thanks for the quick recap. It's amazing how often the question about dates comes up - even from experienced DBA's!