DATEDIFF function | SQL Tutorial and Query Example

Text copied!

DATEDIFF function


  • The syntax of the DATEDIFF function generally looks like this :
    Here's an example of how you might use the DATEDIFF function :

    1. Let's assume we have a table named "[Employees]".

    DATEDIFF(datepart, startdate, enddate)
    Remember :

    Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.

    To enhance your understanding of 'SQL functions', be sure to check out our next tutorial.
    DATEDIFF function
    SELECT *, DATEDIFF(YEAR, Joining_Date, Resign_Date) AS Total_Service
    FROM [Employees];
            
    DATEDIFF function

    Frequently Asked Questions :

    DATEDIFF function in SQL calculates the difference between two dates.
    To calculate the difference between two dates in SQL, use the DATEDIFF function.
    The three arguments for DATEDIFF in SQL are: start date, end date, and the date part (e.g., day, month, year).
    To convert DATEDIFF result to a date in SQL, you typically add or subtract the difference from a date using date manipulation functions like DATEADD.