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

FunctionDescriptionExample Output
CURRENT_DATEReturns the current date (without time)2025-01-29
CURRENT_TIMESTAMPReturns the current date and time2025-01-29 14:30:00
NOW() (MySQL, PostgreSQL)Returns the current timestamp2025-01-29 14:30:00
SYSDATE (Oracle, MySQL)Returns the current system date and time2025-01-29 14:30:00
GETDATE() (SQL Server)Returns the current date and time2025-01-29 14:30:00

Example Query

SELECT CURRENT_DATE, CURRENT_TIMESTAMP, NOW();

2. Extracting Date Parts

FunctionDescriptionExample Output
YEAR(date)Extracts year from a dateYEAR('2025-01-29') → 2025
MONTH(date)Extracts month from a dateMONTH('2025-01-29') → 1
DAY(date)Extracts day from a dateDAY('2025-01-29') → 29
DATEPART(part, date) (SQL Server)Extracts a specific part of a dateDATEPART(DAY, '2025-01-29') → 29
EXTRACT(part FROM date) (PostgreSQL, MySQL)Extracts specific part of a dateEXTRACT(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)

FunctionDescriptionExample Output
DATE_ADD(date, INTERVAL X unit) (MySQL)Adds an interval to a dateDATE_ADD('2025-01-29', INTERVAL 10 DAY) → 2025-02-08
DATE_SUB(date, INTERVAL X unit) (MySQL)Subtracts an interval from a dateDATE_SUB('2025-01-29', INTERVAL 1 MONTH) → 2024-12-29
DATEADD(unit, value, date) (SQL Server)Adds a specified time intervalDATEADD(DAY, 10, '2025-01-29') → 2025-02-08
ADD_MONTHS(date, X) (Oracle)Adds X months to a dateADD_MONTHS('2025-01-29', 2) → 2025-03-29
AGE(date1, date2) (PostgreSQL)Returns difference in years, months, daysAGE('2025-01-29', '2020-01-01') → 5 years 28 days

Example Query

sqlCopyEditSELECT 
    DATE_ADD('2025-01-29', INTERVAL 10 DAY) AS Plus_10_Days,
    DATE_SUB('2025-01-29', INTERVAL 1 MONTH) AS Minus_1_Month;

4. Date Difference

FunctionDescriptionExample Output
DATEDIFF(date1, date2) (MySQL, SQL Server)Returns number of days between two datesDATEDIFF('2025-01-29', '2025-01-01') → 28
TIMESTAMPDIFF(unit, date1, date2) (MySQL)Returns difference in years, months, days, etc.TIMESTAMPDIFF(YEAR, '2000-01-01', '2025-01-29') → 25
AGE(date1, date2) (PostgreSQL)Returns difference in years, months, daysAGE('2025-01-29', '2000-01-01') → 25 years 28 days

Example Query

SELECT 
    DATEDIFF('2025-01-29', '2025-01-01') AS Days_Difference;

5. Formatting Dates

FunctionDescriptionExample Output
DATE_FORMAT(date, format) (MySQL)Formats date as a stringDATE_FORMAT('2025-01-29', '%Y-%m-%d') → 2025-01-29
TO_CHAR(date, format) (PostgreSQL, Oracle)Converts date to a formatted stringTO_CHAR('2025-01-29', 'YYYY-MM-DD') → 2025-01-29
FORMAT(date, format) (SQL Server)Converts date to formatted textFORMAT(GETDATE(), 'yyyy-MM-dd') → 2025-01-29

Example Query

SELECT 
    DATE_FORMAT('2025-01-29', '%W, %M %d, %Y') AS Formatted_Date; 
-- Output: 'Wednesday, January 29, 2025'

6. Extracting Time

FunctionDescriptionExample Output
HOUR(date)Extracts the hourHOUR('2025-01-29 14:30:00') → 14
MINUTE(date)Extracts the minutesMINUTE('2025-01-29 14:30:00') → 30
SECOND(date)Extracts the secondsSECOND('2025-01-29 14:30:15') → 15
TIME(date)Extracts the time portionTIME('2025-01-29 14:30:00') → 14:30:00

Example Query

SELECT 
    HOUR('2025-01-29 14:30:00') AS Hour,
    MINUTE('2025-01-29 14:30:00') AS Minute;

7. Converting Strings to Dates

FunctionDescriptionExample Output
STR_TO_DATE(string, format) (MySQL)Converts a string to a dateSTR_TO_DATE('29-01-2025', '%d-%m-%Y') → 2025-01-29
TO_DATE(string, format) (PostgreSQL, Oracle)Converts a string to a dateTO_DATE('29-01-2025', 'DD-MM-YYYY') → 2025-01-29
CAST(string AS DATE)Converts a string to a dateCAST('2025-01-29' AS DATE) → 2025-01-29
CONVERT(DATE, string, format) (SQL Server)Converts string to dateCONVERT(DATE, '29-01-2025', 105) → 2025-01-29

Example Query

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

Leave a Reply

Your email address will not be published. Required fields are marked *