I pass two date range to my procedure then I select last date of each and every month as below
create procedure stcheck
@From_Date date,
@To_Date date
as
begin
declare @tmpTable table (dates date)
declare @startDate date = @From_Date
declare @endDate date = @To_Date
while @startDate <= @endDate
begin
insert into @tmpTable (dates) values (@startDate)
set @startDate = DATEADD(DAY, 1, @startDate)
end
select max(dates) as [Last day] from @tmpTable as o
group by datepart(YEAR, dates), datepart(MONTH, dates)
If am getting result as follows
Last day
2017-01-31
2017-02-28
2017-03-31
2017-04-30
I need to pass some data to temporary table by using these date output
Ex :-
Insert #xyz
Select * from abcd where postdate <=’2017-01-31’
And my requirement is that these all dates should be automatically used and relevant data should pass to #table.
Hi expert . thank for your early reply.
In brief my requirement is as follows. Please help because I am new with sql.
Think,I have a table as ‘stock’ with in and out stock transaction and stock
report can be taken as follows to any as at date
SELECT item ,SUM(InQty-OutQty) FROM stock
WHERE TransDate <= @AsatDate
GROUP BY item
Having SUM(InQty-OutQty) > 0
If I need to get the each and every month end stock when I am passing the date range
from @From date= ‘2017/05/01’ to @AsatDate=‘2017/09/30’ .
The stock report result should be appear something like this.
Please help what can be done?
NB:- it should be able to pass any date range when it is required.