Category Archives: SQL Server Tutorial

Microsoft SQL Server Tutorials For Beginners, free microsoft sql server tutorial, Authorcode SQL Server tutorial covers the basic concepts of the SQL server with the examples. You can learn the SQL server basic and advanced techniques and how to use sql queries from this tutorial.

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.