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

DECLARE @TT TABLE
(
ID int,
DateOfEntry DateTime
)

INSERT INTO @TT
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'
FROM @TT

OUTPUT

image

1 comment:

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

    ReplyDelete