Wednesday, 30 March 2011

SQL DATEPART



Syntax :DATEPART(datepart,date)

Where date is a valid date expression and datepart can be one of the following:

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


Example:
Assume we have the following "Orders" table:


OrderId
ProductName
OrderDate
1Jarlsberg Cheese2008-11-11 13:23:44.657


The following SELECT statement:


SELECT DATEPART(yyyy,OrderDate) AS OrderYear,
DATEPART(mm,OrderDate) AS OrderMonth,
DATEPART(dd,OrderDate) AS OrderDay,
FROM Orders WHERE OrderId=1


will result in this:

OrderYear
OrderMonth
OrderDay
20081111

No comments:

Post a Comment