Thursday, 13 June 2013

queries of date functions in sqlserver



weekstartdate and weekenddate of current week:

select convert(varchar,DATEADD(dd, -(DATEPART(dw, GETDATE())-1), GETDATE()),103) as [WeekStart]

select CONVERT(varchar, DATEADD(dd, 7-(DATEPART(dw, GETDATE())), GETDATE()),103) as [WeekEnd]

--India Time=UTC+5:30

select convert(varchar,DATEADD(dd, -(DATEPART(dw, DATEADD(mi,30,DATEADD(hh,5,getutcdate())))-1), DATEADD(mi,30,DATEADD(hh,5,getutcdate()))),103) as [WeekStart]

select CONVERT(varchar, DATEADD(dd, 7-(DATEPART(dw, DATEADD(mi,30,DATEADD(hh,5,getutcdate())))), DATEADD(mi,30,DATEADD(hh,5,getutcdate()))),103) as [WeekEnd]


monthstartdate and monthenddate of currentmonth :

select convert(varchar,dateadd(d,-(day(getdate()-1)),getdate()),103) as [monthfirstDate]

select  convert(varchar,dateadd(d,-day(getdate()),dateadd(m,1,getdate())),103)as [monthlastDate]

--India Time=UTC+5:30

select convert(varchar,dateadd(d,-(day(DATEADD(mi,30,DATEADD(hh,5,getutcdate()))-1)),DATEADD(mi,30,DATEADD(hh,5,getutcdate()))),103) as [monthfirstDate]

select  convert(varchar,dateadd(d,-day(DATEADD(mi,30,DATEADD(hh,5,getutcdate()))),dateadd(m,1,DATEADD(mi,30,DATEADD(hh,5,getutcdate())))),103)as [monthlastDate]

No comments:

Post a Comment