sql - query for first and last day of month -
if have name of month, how can have first , last day of month in sql?
i have query returns month names:
declare @startdate datetime, @enddate datetime; select @startdate = '20110501' ,@enddate = '20110801'; select datename(month, dateadd(month, x.number, @startdate)) monthname master.dbo.spt_values x x.type = 'p' , x.number <= datediff(month, @startdate, @enddate)
result:
now, how can first , last day of months? changing query.
try :-
declare @startdate datetime, @enddate datetime; select @startdate = '20110501' ,@enddate = '20110801'; select datename(month, dateadd(month, x.number, @startdate)) monthname, convert(varchar(25), dateadd(dd,-(day(dateadd(month, x.number, @startdate))-1),dateadd(month, x.number, @startdate)),101) firstday, convert(varchar(25),dateadd(dd,-(day(dateadd(mm,1,dateadd(month, x.number, @startdate)))),dateadd(mm,1,dateadd(month, x.number, @startdate))),101) lastday master..spt_values x x.type = 'p' , x.number <= datediff(month, @startdate, @enddate)
result :-
monthname firstday lastday may 05/01/2011 05/31/2011 june 06/01/2011 06/30/2011 july 07/01/2011 07/31/2011 august 08/01/2011 08/31/2011
result obtained taking this query
Comments
Post a Comment