DATEPART() function in SQL Server

 
DATEPART() function returns a value equal to the part of a specified date.

Summery

DATEPART(datepart, date)

where date is the valid date or a string in date format
and datepart can be one of the following:

datepartAbbreviation
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
weekdaydw, w
hourhh
minutemi, n
secondss, s
millisecondms

 

Example

Suppose you have a table ‘tbl_employee’
tbl_employee

Employee IdEmpNameAgeDateOfJoin
1Ankur2811/04/1995
2David3211/04/2002

 

The following query return the date of joining of the Ankur:

SELECT datepart("year", DateOfJoin)

Output will be:

—————–
1995

 

Now if you want to current year, you can use the following query:

SELECT DATEPART("Year",GETDATE())

–or—

SELECT DATEPART("yy",GETDATE())

GETDATE() function return the current date.