How to use sql server DATENAME () Function

Posted By  RRaveen On 14 Dec 2010 23:12:50
emailbookmarkadd commentsprint
No of Views:758
Bookmarked:0 times
Votes:0 times

 Introduction

In my previous tip I have been posted about DATEADD () function. Another function can use for date and time related operation in Sql server called DATENAME (). The DATENAME () returns a character string representing the specified datepart of the specified.date.

DATENAME() Syntax

DATENAME(datepart,datetime)

Datepart: Is the parameter that specifies the part of the date to return.
Datetime: Is an expression that returns a datetime or smalldatetime value.
 
Examples
DECLARE @Today DATETIME
SET @Today=GETDATE() --'15-Dec-2010'


SELECT DATENAME(month, @Today) -- Get the month from date
SELECT DATENAME(mm, @Today )
SELECT DATENAME(m, @Today)
------------Output-----------------------------
----------------------------------------------
--December


SELECT DATENAME(dayofyear, @Today)  -- select day of year, its return current day from 1 of jan 
SELECT DATENAME(dy,@Today)
SELECT DATENAME(y, @Today)
------------Output-----------------------------
-----------------------------------------------
--349

SELECT DATENAME(day, @Today) -- select the day of the month
SELECT DATENAME(dd, @Today)
SELECT DATENAME(d, @Today)
------------Output-----------------------------
-----------------------------------------------
--15

SELECT DATENAME(week, @Today) -- get week numbre of the year
SELECT DATENAME(wk,@Today)
SELECT DATENAME(ww, @Today)
------------Output-----------------------------
-----------------------------------------------
--51

SELECT DATENAME(hour, @Today) -- select the current hour for now
SELECT DATENAME(hh, @Today)
------------Output-----------------------------
-----------------------------------------------
--13

SELECT DATENAME(minute, @Today) -- select the current minute for now
SELECT DATENAME(mi, @Today)
SELECT DATENAME(n, @Today)
------------Output-----------------------------
-----------------------------------------------
--45

SELECT DATENAME(second , @today) -- select the current seocnd for now
SELECT DATENAME(ss, @today)
SELECT DATENAME(s, @today)
------------Output-----------------------------
-----------------------------------------------
--34

SELECT DATENAME(millisecond , @today) -- select the current milliseconds for now
SELECT DATENAME(ms, @today)
------------Output-----------------------------
-----------------------------------------------
--340

Hopes help.thank you for reading.

 

Sign Up to vote for this article
Other popular Tips/Tricks
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top