SQL > SQL Date Functions > Datepart Function

DATEPART is a SQL Server function that extracts a specific part of the date/time value. Its syntax is as follows:

DATEPART (part_of_day, expression)

where part_of_day can have the following:

datepartabbreviation
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns
TZoffsettz
ISO_WEEKisowk, isoww

Example 1

SELECT DATEPART (yyyy, '2000-01-20');

Result:

2000

Example 2

SELECT DATEPART (dy, '2000-02-10');

Result:

41

2000-02-10 is the 41st day in the year 2000.

Next: SQL GETDATE




Copyright © 2014   1keydata.com   All Rights Reserved.     Privacy Policy     About   Contact