Write a Sql Query to Find the Oldest Date of Birth
Problem:
You want to sort the rows by date.
Example 1:
The exam table has two columns, subject and exam_date.
| Subject | ExamDate |
|---|---|
| Mathematics | 2019-12-19 |
| English | 2020-01-08 |
| Science | 2020-01-05 |
| Health | 2020-01-05 |
| Art | NULL |
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):
| Subject | ExamDate |
|---|---|
| Art | NULL |
| Science | 2020-01-05 |
| Health | 2020-01-05 |
| English | 2020-01-08 |
| Mathematics | 2019-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.
| Subject | ExamYear | ExamMonth | ExamDay |
|---|---|---|---|
| Mathematics | 2019 | December | 19 |
| English | 2020 | January | 8 |
| Science | 2020 | January | 5 |
| Health | 2020 | January | 5 |
| Art | NULL | NULL | NULL |
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):
| Subject | ExamYear | ExamMonth | ExamDay |
|---|---|---|---|
| Art | NULL | NULL | NULL |
| Health | 2020 | January | 5 |
| Science | 2020 | January | 5 |
| English | 2020 | January | 8 |
| Mathematics | 2019 | December | 19 |
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;
Write a Sql Query to Find the Oldest Date of Birth
Source: https://learnsql.com/cookbook/how-to-order-by-date-in-t-sql/
0 Response to "Write a Sql Query to Find the Oldest Date of Birth"
Postar um comentário