How to Order by Date in T-SQL Database: T-SQL Operators: ORDER BY CAST 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. 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: SQL Basics SQL Basics in PostgreSQL SQL Basics in SQL Server SQL Practice Set Recommended articles: How to Group Data by Week in SQL Server How to Get the First Day of the Week in SQL Server What Does ORDER BY Do? A Detailed Guide to SQL ORDER BY Microsoft SQL Server Pros and Cons See also: How to Get the Day from a Date in T-SQL How to Change Date and Time Formats in T-SQL How to Add Days to a Date in T-SQL How to Get the Month from a Date in T-SQL Subscribe to our newsletter Join our monthly newsletter to be notified about the latest posts. Email address How Do You Write a SELECT Statement in SQL? What Is a Foreign Key in SQL? Enumerate and Explain All the Basic Elements of an SQL Query