Sunday, December 30, 2012

DATE PART function Examples in SQL


·         DATE PART function Examples:

select getdate() as Currentdate
--   Returns current date.

select datepart(month,getdate()) as monthnumber
--   Returns month number(m,mm)

select datename(month,'2/12/2008')as monthname  
--   Returns name of the month.

select datepart(m,0),datepart(d,0),datepart(yy,0)
--   returns default date 1 jan 1900

select DATEPART(ww,'jan 24, 2008')
--   returns no of weeks.(ww/wk)

select DATEPART(dw, '2/5/2009')
--   returns day of the week considers sunday=1 and saturday=7

select DATEPART(yy,'12/29/80')
--   returns year....50 it shows 1950

select DATEPART(qq, '10/29/2009')
--   returns the qurter number in a year for present date (q).

select DATEPART(dy, '1/5/2008')
--   Returns the day of year i.e from 1 to 365 (dy/y)

select DATEPART(dd, '12/29/2008')
--   Returns the specified date.(dd/d)

select DATEPART(hh, '12/29/2008 19:25:45:44')
--   returns no. of hours.

select DATEPART(mi, '12/29/2008 19:25:45:44')
--   returns no.of minutes.(mi/n)

select DATEPART(ss, '12/29/2008 19:25:45:44')
--   returns no.of seconds.(ss/s)

select DATEPART(hh, getdate())
--   returns no.of miliseconds.(ms)

No comments:

Post a Comment