SQL provides various date functions to manipulate and extract information from date and time values. These functions vary slightly across different databases (MySQL, PostgreSQL, SQL Server, Oracle), but the core functionalities remain similar.
1. Getting Current Date and Time
Function
Description
Example Output
CURRENT_DATE
Returns the current date (without time)
2025-01-29
CURRENT_TIMESTAMP
Returns the current date and time
2025-01-29 14:30:00
NOW() (MySQL, PostgreSQL)
Returns the current timestamp
2025-01-29 14:30:00
SYSDATE (Oracle, MySQL)
Returns the current system date and time
2025-01-29 14:30:00
GETDATE() (SQL Server)
Returns the current date and time
2025-01-29 14:30:00
Example Query
SELECT CURRENT_DATE, CURRENT_TIMESTAMP, NOW();
2. Extracting Date Parts
Function
Description
Example Output
YEAR(date)
Extracts year from a date
YEAR('2025-01-29') → 2025
MONTH(date)
Extracts month from a date
MONTH('2025-01-29') → 1
DAY(date)
Extracts day from a date
DAY('2025-01-29') → 29
DATEPART(part, date) (SQL Server)
Extracts a specific part of a date
DATEPART(DAY, '2025-01-29') → 29
EXTRACT(part FROM date) (PostgreSQL, MySQL)
Extracts specific part of a date
EXTRACT(YEAR FROM '2025-01-29') → 2025
Example Query
SELECT
YEAR('2025-01-29') AS Year,
MONTH('2025-01-29') AS Month,
DAY('2025-01-29') AS Day;
3. Date Arithmetic (Add/Subtract Days, Months, Years)
SELECT STR_TO_DATE('29-01-2025', '%d-%m-%Y') AS Converted_Date;
Conclusion
SQL provides powerful date manipulation functions for:
✅ Getting current date/time ✅ Extracting date parts (YEAR, MONTH, DAY) ✅ Adding/subtracting time intervals ✅ Calculating date differences ✅ Formatting and converting dates