"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, December 29, 2010

sql server DATENAME () Function

DATENAME () 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
I got this stuff from codegain.com, by understanding its importance I’m republishing it.
Have a nice day... 'N happy Coding :)

No comments: