Monday, 20 May 2013

Sql server: DateName function in details

DateName Function

Sql server:DateName function indetail

            This article explains DATENAME function with different examples in sql server.
DATENAME function is used to get string of the specified datepart of the specified date.

Syntax of  DATENAME Function :

DATENAME ( datepart ,date )

Datepart specifies the part of the date to return.Date is an expression that returns a datetime
or smalldatetime value or a character string in a date format.

Return type of DATENAME function is nvarchar .

Examples::

GET YEAR FROM CURRENT DATE


  select
DateName(year,getdate());
  select DateName(yy,getdate());
  select DateName(yyyy,getdate());

Get Quarter From current date


  select
DateName(quarter,getdate());
  select DateName(qq,getdate());
  select DateName(q,getdate());

Get month name From current date


  select DateName(month,getdate());
  select DateName(mm,getdate());
  select DateName(m,getdate());

Get day number of year From current date


select
DateName(dayofyear,getdate());
select DateName(dy,getdate());
select DateName(y,getdate());

Get day in int From current date


  select
DateName(day,getdate());
  select DateName(dd,getdate());
  select DateName(d,getdate());

Get date name e.g -monday From current date


  select
DateName(weekday,getdate());
  select DateName(dw,getdate());

Get hour From current date


  select
DateName(hour,getdate());
  select DateName(hour,getdate());
  select DateName(hh,getdate());
  select DateName(hh,getdate());

Get minute From current date


  select
DateName(minute,getdate());
  select DateName(mi,getdate());
  select DateName(n,getdate());

Get second From current date


  select
DateName(second,getdate());
  select DateName(ss,getdate());
  select DateName(s,getdate());

Get millisecond From current date


  select DateName(millisecond,getdate());
  select DateName(ms,getdate());

1 comment:

  1. Can we use datepart function instead of datename?
    Select datepart(year,getdate())

    ReplyDelete

8451831077