![]() |
VOOZH | about |
SQL (Structured Query Language) can work with datetime data types. SELECT DATE is an important concept when retrieving records that filter data based on date. Whether you work with employee records, transaction records, or product sales, modifying and retrieving date data is often important for your SQL queries.
In this article, we’ll explore how to use the SELECT DATE functionality in SQL. We will explain how to retrieve and manipulate date values using SQL queries, including DATE, DATETIME, TIMESTAMP, and related SQL functions.
In SQL, date and time values are stored in DATE, DATETIME, TIME, TIMESTAMP, and YEAR data types, depending on your use case and the database system you're using. To retrieve these values, we use the SELECT statement.
Syntax:
SELECT column_name
FROM table_name
WHERE date_column = 'YYYY-MM-DD';
Let's create an Employee table with a column hire_date of type DATE, which stores the hiring date of employees.
CREATE TABLE Employee (
EmpID INT,
FirstName VARCHAR(255),
LastName VARCHAR(255),
HireDate DATE
);
INSERT INTO Employee (EmpID, FirstName, LastName, HireDate)
VALUES
(1, 'John', 'Doe', '2022-01-15'),
(2, 'Jane', 'Smith', '2021-06-30'),
(3, 'Alice', 'Johnson', '2020-08-25');Output:
| EmpID | FirstName | LastName | HireDate |
|---|---|---|---|
| 1 | John | Doe | 2022-01-15 |
| 2 | Jane | Smith | 2021-06-30 |
| 3 | Alice | Johnson | 2020-08-25 |
SELECT FirstName, LastName, HireDate
FROM Employee
WHERE HireDate = '2022-01-15';Output:
| FirstName | LastName | HireDate |
|---|---|---|
| John | Doe | 2022-01-15 |
This SQL query retrieves the FirstName, LastName, and HireDate of the employee who was hired on 2022-01-15.
These SQL functions allow you to extract specific components (like year, month, or day) from a DATE or DATETIME column.
SELECT FirstName, LastName, HireDate
FROM Employee
WHERE YEAR(HireDate) = 2021;Output:
| FirstName | LastName | HireDate |
|---|---|---|
| John | Doe | 2022-01-15 |
This query retrieves all employees hired in the year 2022.
SELECT FirstName, LastName, HireDate
FROM Employee
WHERE MONTH(HireDate) = 6;
Output:
| FirstName | LastName | HireDate |
|---|---|---|
| Jane | Smith | 2021-06-30 |
In addition to extracting parts of a date, SQL provides several advanced date functions for more complex operations. Below are some common advanced functions:
SELECT FirstName, LastName, HireDate
FROM Employee
WHERE HireDate < CURDATE();This query retrieves all employees hired before today’s date.
The BETWEEN operator allows you to filter records within a specified date range. This can be useful when you need to retrieve data between two specific dates, such as all transactions made within a certain period or all employees hired during a specific quarter.
SELECT FirstName, LastName, HireDate
FROM Employee
WHERE HireDate BETWEEN '2021-01-01' AND '2022-01-01';Output:
| FirstName | LastName | HireDate |
|---|---|---|
| Jane | Smith | 2021-06-30 |
SELECT DATE is an essential concept in SQL, especially when dealing with time-based data. By mastering SQL date functions, such as YEAR(), MONTH(), DAY(), CURDATE(), DATE_ADD(), and DATE_SUB(), you can effectively filter, manipulate, and retrieve date and time data based on your requirements. With the right use of these functions, you can write efficient SQL queries for tasks ranging from simple date comparisons to advanced time-based analysis. Whether you're tracking sales, employee records, or any other time-sensitive data, understanding how to use dates in SQL queries will help you extract the precise data you need.