Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dealing with dates
Working with time data
Date and time date
Extracting dates and times
26. The DATENAME() function
Doing arithmetic with dates
Converting date and time data
Building date and time data from parts
Summary and review

Instruction

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.

Exercise

For each discontinued aircraft, show its Id and the day of the week (by name) when it was discontinued. Name the column Weekday. Use DATENAME(weekday, date).

Stuck? Here's a hint!

Remember that all the discontinued aircraft don't have NULL in the DiscontinuationDatetime column.