datepart

Description

Returns the integer value of the specified part of a datetime value.

Syntax

datepart(date_part, date)

Parameters

date_part

is a date part. Table 2-5 lists the date parts, the abbreviations recognized by datepart, and the acceptable values.

Table 2-5: Date parts and their values

Date part

Abbreviation

Values

year

yy

1753 – 9999 (2079 for smalldatetime)

quarter

qq

1 – 4

month

mm

1 – 12

week

wk

1 – 54

day

dd

1 – 31

dayofyear

dy

1 – 366

weekday

dw

1 – 7 (Sun. – Sat.)

hour

hh

0 – 23

minute

mi

0 – 59

second

ss

0 – 59

millisecond

ms

0 – 999

calweekofyear

cwk

1 – 53

calyearofweek

cyr

1753 – 9999

caldayofweek

cdw

1 – 7

When you enter a year as two digits (yy):

date

can be either the function getdate, a character string in an acceptable date format, an expression that evaluates to a valid date format, or the name of a datetime or smalldatetime column.

Examples

Example 1

This example assumes a current date of November 25, 1995:

select datepart(month, getdate())

-----------
          11

Example 2

select datepart(year, pubdate) from titles where type = "trad_cook"

 -----------
        1990 
        1985 
        1987 

Example 3

select datepart(cwk,’1993/01/01’)

-----------
          53

Example 4

select datepart(cyr,’1993/01/01’)

-----------
        1992

Example 5

select datepart(cdw,’1993/01/01’)

-----------
           5

Usage

Standards

SQL92 – Compliance level: Transact-SQL extension.

Permissions

Any user can execute datepart.

See also

Datatypes Date and time datatypes

Commands select, where clause

Functions dateadd, datediff, datename, getdate