DATEPART function | SQL Tutorial and Query Example

Text copied!

DATEPART function


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

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

    DATEPART(datepart, date)
    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.
    DATEPART function
    SELECT *, DATEPART(YEAR, Joining_Date) AS Joining_Year
    FROM [Employees];
            
    DATEPART function

    Frequently Asked Questions :

    The DATEPART function in SQL extracts a specific part of a date or time, such as year, month, day, etc.
    DATEPART DW function in SQL extracts the day of the week (Sunday = 1, Monday = 2, etc.) from a date.
    To get only the date part in SQL, you can use functions like CAST or CONVERT to strip off the time portion.
    To extract MM (month) and YYYY (year) from a date in SQL, you can use functions like MONTH() and YEAR().