Good. Let's look at a similar function, DATENAME
:
SELECT
DATENAME(datepart, date)
As you see, it takes the same parameters as DATEPART
. The datepart value can be any of the units we already discussed, and the date can be a string, a field, or an expression. The difference is that it returns the result as a string rather than an integer. If, for example, the month value was 05
, DATENAME
would return 'May'
instead of 5
.
Look at this example:
SELECT
DATENAME(year, Date) AS Year,
DATENAME(month, Date) AS Month,
AVG(Delay) AS AvgDelay
FROM Flight
GROUP BY DATENAME(year, Date),
DATENAME(month, Date);
This query returns three columns: one with the year, the second with month (by name), and the third with the average delay.