DATEPART() Function in SQL Server
Last Updated :
18 Jan, 2021
DATEPART() function :
This function in SQL Server is used to find a given part of the specified date. Moreover, it returns the output value as an integer.
Features :
- This function is used to find a given part of the specified date.
- This function comes under Date Functions.
- This function accepts two parameters namely interval and date.
- This function can also include time in the date section.
- This function returns the output in integer form.
Syntax :
DATEPART(interval, date)
Parameter :
This method accepts two parameters as given below as follows.
- interval -
It is the specified part which is to be returned. Moreover, the values of the interval can be as given below.
year, yyyy, yy = Year, which is the specified year.
quarter, qq, q = Quarter, which is the specified quarter.
month, mm, m = month, which is the specified month.
dayofyear, dy, y = Day of the year, which is the specified day of the year.
day, dd, d = Day, which is the specified day.
week, ww, wk = Week, which is the specified week.
weekday, dw, w = Weekday, which is the specified week day.
hour, hh = hour, which is the specified hour.
minute, mi, n = Minute, which is the specified minute.
second, ss, s = Second, which is the specified second.
millisecond, ms = Millisecond, which is the specified millisecond.
- date -
It is the specified date which is to be used.
Returns :
It returns a given part of the specified date.
Example-1 :
Using DATEPART() function and getting the year part of the specified date.
SELECT DATEPART(year, '2017/08/25');
Output :
2017
Example-2 :
Using DATEPART() function and getting the month part of the specified date.
SELECT DATEPART(month, '2017/08/25');
Output :
8
Example-3 :
Using DATEPART() function and getting the day part of the specified date.
SELECT DATEPART(day, '2017/08/25');
Output :
25
Example-4 :
Using DATEPART() function and getting the hour part of the specified date which includes time as well.
SELECT DATEPART(hour, '2021/01/06 05:30');
Output :
5
Example-5 :
Using DATEPART() function and getting the second part of the specified date which includes time as well using a variable.
DECLARE @date VARCHAR(50);
SET @date = '2019/06/05 07:37:54';
SELECT DATEPART(second, @date);
Output :
54
Application :
This function is used to find the given part of the specified date.
Similar Reads
DATEFROMPARTS() Function in SQL Server
DATEFROMPARTS() function : This function in SQL Server is used to return a date from the given values of year, month and day. Features : This function is used to find a date from the stated values of year, month and day. This function comes under Date Functions. This function accepts three parameter
2 min read
DATEADD() Function in SQL Server
DATEADD() function : This function in SQL Server is used to sum up a time or a date interval to a specified date, then returns the modified date. Features : This function is used to sum up a time or a date interval to a date specified. This function comes under Date Functions. This function accepts
3 min read
DATENAME() Function in SQL Server
DATENAME() function : This function in SQL Server is used to find a given part of the specified date. Moreover, it returns the output value as a string. Features : This function is used to find a given part of the specified date.This function comes under Date Functions.This function accepts two para
2 min read
DAY() Function in SQL Server
DAY() function : This function in SQL Server is used to return the day of the month i.e, from 1st to 31st for date stated. Features : This function is used to find the day of the month for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, date.
2 min read
ATN2() Function in SQL Server
In this article, we are going to cover the ATN2()function in which we will see how we can get the arc tangent of two given numbers. Let's discuss one by one. // here val1 and val2 are input. Input : ATN2(val1, val2) Output : Arc tangent result. ATN2() : It is the function that returns the arc tangen
1 min read
SQL Server DATEDIFF() Function
The DATEDIFF() function in SQL Server is a powerful tool used to calculate the difference between two dates or times. It returns an integer representing the number of date or time boundaries crossed between the specified dates, based on the specified date. This function is essential for tasks that i
3 min read
SQL Server FORMAT() Function
SQL Server FORMAT() function formats the specified value in the given format. FORMAT Function in SQL ServerThe FORMAT function in SQL Server is used to format date/time and number values with locale-aware formatting. The FORMAT function achieves various formatting requirements, showing dates in spe
2 min read
TAN() and COT() Function in SQL Server
1. TAN() Function : The TAN() function returns the tangent of a number. Syntax : TAN(number) Parameter : Required. A numeric value. number : It is a numeric value. Returns : It returns the float_expression of the tangent of a number. Example-1 : When the arguments holds a positive number. SELECT TAN
2 min read
SQL Server ISNULL() Function
The ISNULL() function in SQL Server is a powerful tool for handling NULL values in our database queries. It allows us to replace NULL values with a specified replacement value, ensuring that your queries return meaningful results even when data is missing.In this article, We will learn about the SQL
3 min read
LOG() Function in SQL Server
The LOG() function returns the logarithm of a specified number or the logarithm of the number to the specified base. Syntax : LOG(number, base) Parameter : LOG() function accepts two-parameters as mentioned above and described below. number - This parameter hold a number which is greater than 0. bas
1 min read