Getting Previous month Particular dates in Sql

Posted by Ravianand under Sql Server on 11/7/2012 | Points: 10 | Views : 925 | Status : [Member] | Replies : 3
Getting Previous and current month Particular dates While Passing Month and Year as input.

Ex: Month=Sep year=2012

It should display as:

26/10/2012
27/10/2012
.....

25/11/2012

Regards,
Ravi



Responses

Posted by: Krv on: 11/7/2012 [Member] Starter | Points: 25

Up
0
Down
You can use DATEADD and DATEPART SQL function

Thanks,
R.Vasanth
MCTS(SQL Server & BI)
http://bigdatatech.blogspot.com

Ravianand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ravianand on: 11/7/2012 [Member] Starter | Points: 25

Up
0
Down
Not Getting Solution.

Regards,
Ravi

Ravianand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 11/7/2012 [Member] Starter | Points: 25

Up
0
Down
hi all...


please refer to the follwing code....it helps you ....


Add the value in @month and @year accordingly...

Declare @month int


Declare @year int

set @month = 10
set @year = 2012


Declare @temp table(
[date] datetime
)

Declare @day int

Select @day = DATEPART(D,GETDATE())

Declare @present_startdate datetime

set @present_startdate=CONVERT(datetime,cast(@day as varchar)+'/'+cast(@month as varchar)
+'/'+cast(@year as varchar),103)

declare @i int

set @i = 0

Declare @previous_month_startdate datetime

set @previous_month_startdate = dateadd(M,-1,@present_startdate)

while datediff(D,@previous_month_startdate,@present_startdate) <> 0
begin

set @previous_month_startdate =DATEADD(D,1,@previous_month_startdate )

Insert into @temp values(@previous_month_startdate )

set @i= @i+1
end

select CONVERT(varchar,[date],103) from @temp





Sourabh07

Ravianand, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response