SQL DATEPART
SQL DATEPART function is used to extract or display specified date part from the existing date. This function always return integer value. For example, If you want to extract year, month or quarter from the existing Date, you can use this SQL Datepart function.
SQL DATEPART Syntax
Datepart: It is the part of a given date which we are going to display as output. Following table will display the list of available datepart argument in SQL Server 2014
Datepart | Abbreviations | Description |
---|---|---|
year | yy,yyyy | This will display the year value from the given date |
quarter | qq, q | This will find and display the quarter number from the given date |
month | mm, m | This will find and display the month number from the given date |
dayofyear | dy, y | This will find and display the day of a year number from the given date (1 to 365) |
day | dd, d | This will find and display the day number from the given date (1 to 31) |
week | wk, ww | This will display the Week number from the given date |
weekday | dw, w | This will display the Week days number from the given date (0 as Sunday & 6 as Saturday) |
hour | hh | This will display the Hour value present in the given date |
minute | mi, n | This will display the Minute Value present in the given date |
second | ss, s | This will display the Seconds Value present in the given date |
millisecond | ms | This will display the Milliseconds Value present in the given date |
microsecond | mcs | This will display the microsecond Value present in the given date |
nanosecond | ns | This will display the Nanoseconds Value present in the given date |
TZOffset | tz | This will display the Time Zone Offset Value present in the given date |
ISO_WEEK | isowk, isoww | This will display the Iso Week Number from the given date |
Date: Please specify the valid date as second argument. It can be column, expression or any variable.
SQL DATEPART Function Example 1
In this example we are going to declare a variable of datetime2 data type. Let us assign valid date to that variable and perform all the available datepart operation
SQL CODE
OUTPUT
ANALYSIS
We declared one variable and assigned the date and time to that variable
We asked the SQL Datepart function to display the year value from the given date
We asked the SQL Datepart function to display the Month number from the given date
and so on
SQL DATEPART Function Example 2
In this example, we are going to use one of the custom SQL table to perform SQL Datepart operations on Hire Date column.
SQL CODE
OUTPUT
ANALYSIS
Below statement will display the Year value from the Hire Date Column
Below statement will display the Quarter value from the Hire Date Column
Thank You for Visiting Our Blog