Back to cookbooks list Articles Cookbook

How to Order by Date in T-SQL

Problem:

You want to sort the rows by date.

Example 1:

The exam table has two columns, subject and exam_date.

SubjectExamDate
Mathematics2019-12-19
English2020-01-08
Science2020-01-05
Health2020-01-05
ArtNULL

You want to sort the rows by exam_date.

Download SQL Server Cheat Sheet

Solution:

SELECT *
FROM Exam
ORDER BY ExamDate;

The result looks like this (the rows are sorted in ascending order by ExamDate):

SubjectExamDate
ArtNULL
Science2020-01-05
Health2020-01-05
English2020-01-08
Mathematics2019-12-19

Discussion:

Use the ORDER BY keyword and the name of the column by which you want to sort. This way, you'll sort the data in ascending order by this column. You could also use the ASC keyword to make it clear that the order is ascending (the earliest date is shown first, the latest date is shown last, etc.).

SELECT *
FROM Exam
ORDER BY ExamDate ASC;

If you'd like to see the latest date first and the earliest date last, you need to sort in descending order. Use the DESC keyword in this case.

SELECT *
FROM Exam
ORDER BY ExamDate DESC;

Note that in T-SQL, NULLs are displayed first when sorting in ascending order and last when sorting in descending order. Also, the rows with the same ExamDate are displayed in non-deterministic order (you may see Science second and Health third, or Health second and Science third).

Example 2:

The exam table has the following columns: subject, exam_year, exam_month, and exam_day. The month is given by name, not by number.

SubjectExamYearExamMonthExamDay
Mathematics2019December19
English2020January8
Science2020January5
Health2020January5
ArtNULLNULLNULL

You want to sort the rows by exam date.

Solution:

SELECT *
FROM Exam
ORDER BY CAST(
  CAST(ExamYear AS VARCHAR(4))
  + '-'
  + ExamMonth
  + '-'
  + CAST(ExamDay AS VARCHAR(2))
AS DATE);

The result looks like this (the rows are sorted in ascending order by ExamYear, ExamMonth, and ExamDate):

SubjectExamYearExamMonthExamDay
ArtNULLNULLNULL
Health2020January5
Science2020January5
English2020January8
Mathematics2019December19

Discussion:

To group by date, create date values from the year, the month, and the day values. To do this, use the CAST() function. If you have a date stored as a string in the 'YYYY-Month-DD' format, you can cast it to a date using CAST(date_string AS date). First, you need to create a string, also using the CAST() function:

CAST(ExamYear AS VARCHAR(4))
+ '-'
+ ExamMonth
+ '-'
+ CAST(ExamDay AS VARCHAR(2))

The expression CAST(ExamYear AS VARCHAR(4)) creates a string from the number stored in ExamYear. The expression CAST(ExamDay AS VARCHAR(2)) creates a string from the number stored in ExamDay. ExamMonth is already a string, so there's no need to cast it.

Then, you need to cast this string to a date using the CAST(date_string AS date) function:

CAST(
  CAST(ExamYear AS VARCHAR(4))
  + '-'
  + ExamMonth
  + '-'
  + CAST(ExamDay AS VARCHAR(2))
AS DATE)

Use it with an ORDER BY clause to sort the rows in ascending order by date. If you'd like to see the rows in descending order, just append a DESC keyword, like this:

SELECT *
FROM Exam
ORDER BY CAST(
  CAST(ExamYear AS VARCHAR(4))
  + '-'
  + ExamMonth
  + '-'
  + CAST(ExamDay AS VARCHAR(2))
AS DATE) DESC;

Recommended courses:

Recommended articles:

See also: