I have a Query: SELECT * FROM tble WHERE BILL_DATE BETWEEN '01-NOV-16' AND '01-NOV-16' Now i need to know if bellow thing can be arranged For i=1 to 30 Loop SELECT * FROM tble WHERE date BETWEEN 'i-NOV-16' AND 'i-NOV-16' Insert Data in a table [SQL Server] Next i Thanks, Sami
Step back and think about the data in a complete set rather than 30 loops. You have a query that retrieves one day's worth of data, and you want a month's worth, so you are attempting to reuse the 'day' query for the number of days in the month. Don't do it. Write the query to retrieve the data you want, for example select * from tble where date between '01-nov-16' and '30-nov-16' OK. But you might say that you don't always want a complete month. You might want 20 days worth of data starting from some given start date! Again, describe the result set.... declare @startdate date set @startdate = '19 October 2016' select * from tble where date between @startdate and dateadd(day, 20, @startdate) And from this you could even make the 'number of days' a parameter..... But always describe the result set you want SQL to return, rather than describing *how* to get the results.