To compare dates in SQL, you can use various operators such as =
, >
, <
, >=
, <=
, and <>
.
Examples:
- Equal to a specific date:
sql
SELECT * FROM table WHERE date_column = '2024-12-29';
- Greater than a specific date:
sql
SELECT * FROM table WHERE date_column > '2024-12-29';
- Less than a specific date:
sql
SELECT * FROM table WHERE date_column < '2024-12-29';
- Between two dates:
sql
SELECT * FROM table WHERE date_column BETWEEN '2024-01-01' AND '2024-12-31';
- Comparing date and time:
sql
SELECT * FROM table WHERE datetime_column >= '2024-12-29 10:00:00';
- Note: Ensure the date format matches the database’s date format (
YYYY-MM-DD
for most SQL databases).