The DATEPART function on SQL Server
February 3, 2012 1 Comment
The DATEPART function.
This function will take a date/time expression and return a single part of the date, such as hour, minute, month, day, etc. The syntax for using DATEPART is:
Quote
Where the datepart is one of the following:
- Year: yy, yyyy
- Quarter: qq, q
- Month: mm, m
- dayofyear: dy, y
- Day: dd, d
- Week: wk, ww
- Weekday: dw
- Hour: hh
- Minute: mi, n
- Second: ss, s
- Millisecond: ms.
let say the date right now is 23-03-2012
if we want to display the day, use :
DECLARE @date DATETIME SET @date = '2012-03-23 10:31 PM' SELECT DATEPART(dd,@date) --- the query will return = 23 ---------------------------------------
if we want to display the Month, use :
DECLARE @date DATETIME SET @date = '2012-03-23 10:31 PM' SELECT DATEPART(mm,@date) --- the query will return = 03 ---------------------------------------
if we want to display the Year, use :
DECLARE @date DATETIME SET @date = '2012-03-23 10:31 PM' SELECT DATPART(yyyy,@date) --- the query will return = 2012 ETC
I just want to tell you that I’m newbie to weblog and truly liked your web page. Almost certainly I’m going to bookmark your website . You really come with beneficial stories. Kudos