SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Loop Date Range


Loop Date Range

Author
Message
RubyRed
RubyRed
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 55
I am trying to write a store procedure that will extract data from the 1st of the month up-to current date within that month - ending on the last day of the month. For instances today is the 23rd - I need to extract data from a table where the date starting from the 1st through the 23rd, however I must extract ONE DAY at a time, therefore the query must loop through the dates.

I have a very complexed query already in place that calls on several functions - I can only process 1 day at a time.

How do I reloop through a query extracting data from the 1st up-to the current date one day at a time?
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5810 Visits: 11771
Did you have a question that you wanted to ask?
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18768 Visits: 14899
Why must you return only 1 row at a time? Couldn't you return the data as a set and let the business layer or UI handle the looping? IMO that would be more effecient, as you would have one call to the DB and the rest done in memory. If you post some DDL, test data, and expected/desired results as mentioned in the link in my signature then you will likely get some solutions proposed.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7817 Visits: 9971
Why do you have to loop? What is the requirement that states you have to process a single day at a time?

FWIW, you could build a stored procedure that accepts as input the date you want to search, and query for everything on that one date. Example:


CREATE PROCEDURE dbo.MySingleDaySearch
@inputDate datetime
AS

DECLARE @startDate datetime;
DECLARE @endDate datetime;

SET @startDate = dateadd(day, datediff(day, 0, @inputDate), 0); -- remove time portion
SET @endDate = dateadd(day, 1, @startDate); -- set the end date to tomorrow

SELECT ...
FROM dbo.MyTable t
WHERE t.MyDateColumn >= @startDate
AND t.MyDateColumn < @endDate;
GO



But, I gotta say that running this for a full month by 'looping' on each date is not the right way to solve the problem. If you posted the problem you are trying to solve - it might be easier for us to recommend a better solution than looping.

Jeffrey Williams
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search