The
first example displays the date in 2003/01/22 10:31PM format. This can be
done with the following simple script that uses the CONVERT and SUBSTRING
functions. Note this example uses the 111 style format of the CONVERT function.
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT CONVERT(CHAR(11),@DT,111) +
SUBSTRING(CONVERT(CHAR(19),@DT,100),13,19)
Normally
there is always more than one way to get the same results. Here is another way
to get the same date displayed by using the DATEPART, CAST and RIGHT functions:
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT CAST(DATEPART(YYYY,@DT) AS CHAR(4)) + '/'
+ RIGHT(CAST(100+DATEPART(MM,@DT) AS CHAR(3)),2) + '/'
+ RIGHT(CAST(100+DATEPART(DD,@DT) AS CHAR(3)),2) + ' '
+ CASE WHEN DATEPART(HH,@DT) < 13
THEN RIGHT(CAST(100+DATEPART(HH,@DT) AS CHAR(3)),2)
ELSE CAST(DATEPART(HH,@DT)-12 AS CHAR(2))
END + ':'
+ RIGHT(CAST(100+DATEPART(MI,@DT) AS CHAR(3)),2)
+ CASE WHEN DATEPART(HH,@DT) < 12
THEN 'AM'
ELSE 'PM'
END
The
next example will display the date in 2003/01/22 22:31 format. This example
uses the REPLACE function to convert the dashes, of CONVERT style 120, to
slashes.
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT REPLACE(CONVERT(CHAR(16),@DT,120),'-','/')
The
following example will display the date in 22-01-2003
format, without the time portion. To display only the date portion and
truncate the time part, I specified an output data type and length. In the
example below a CHAR(10) was used for data type and length. This will cause
the CONVERT function to display only the first 10 characters of style 105.
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT CONVERT(CHAR(10),@DT,105)
The
next example displays the date using 22 January 2003
format. To produce this date output, I will introduce a new function,
DATENAME. The DATENAME function returns a character string representing the
requested part of the date. The DATENAME function uses the following syntax:
DATENAME( datepart , date )
Where
the datepart is one of the following: Year, yy, yyyy, quarter, qq, q,
month, mm, m, dayofyear, dy, y, day, dd, d, week, wk, ww, weekday, dw,hour, hh,
minute, mi, n, second, ss, s, millisecond, or ms, and date is a valid
date expression. This function will return a character string representing the
particular date part requested. Be aware that the "weekday" parm returns the
day name, like Sunday, Monday, Tuesday, etc, and the "month" parm returns the
month name, like January, February, March, etc. This example uses DATENAME to
return the DAY, MONTH and YEAR of the variable @DT. The month portion of the
date will be returned with the spelled out version of the month, or in this
case "January."
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT DATENAME(DAY,@DT) + ' ' + DATENAME(MONTH,@DT) + ' ' + DATENAME(YEAR, @DT)
The
CONVERT function alone allows me to output a date with Jan 22 2003 10:13PM
format. By using a style of 100, the CONVERT function displays the date format
needed.
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT CONVERT(CHAR(19),@DT,100)
To
display a date in January 22, 2003 format I will again use the DATENAME function, I
just change the output formatting, by rearranging the calls for each date
part.
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT DATENAME(MONTH,@DT) + ' ' + DATENAME(DAY,@DT) + ', ' + DATENAME(YEAR, @DT)
Three
more SQL Server functions that might help you with formatting date output are
worth mentioning. These functions are DAY, MONTH, and YEAR. Each of these
functions accepts a date expression, and returns an integer value representing
the day, month or year depending on which function is being called. Here is an
example of how these functions are used to return a date in 01/22/2003 format.
DECLARE @DT DATETIME
SET @DT = '2003-01-22 10:31 PM'
SELECT '0' + CAST(MONTH(@DT) AS CHAR(1)) + '/' + CAST(DAY(@DT) AS
CHAR(2)) + '/' + CAST(YEAR(@DT) AS CHAR(4))
Conclusion
There
are many different way to display a given date and time value. Because of this,
SQL Server provides the CONVERT function to format date/time values into a
number of preset output formats. Also by using the CONVERT function along with
one or more additional SQL Server functions, such as DATEPART you should be
able to display the date any way you need.
My
next article in this date/time series will discuss searching SQL Server tables
to find records based on a date and time value.
»
See All Articles by Columnist Gregory A. Larsen