Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2006
    Posts
    111

    Unanswered: For Loop / Iterate

    how do I loop through a variable like the following in c++

    for(int i=0;i<10;i++)
    {
    //doSomething;
    }

    basically I want to iterate through a certain number of years. I expect to be given a variable period in years, say 2003 to 2006. then i will append the months to get the full calendar years '01/01/2001' and '12/31/2001' and execute my select block using the start and end dates formed... so pseudocode will be like:

    Code:
    DECLARE @StartYr int
    DECLARE @EndYr int
    DECLARE @StartDate string
    DECLARE @EndDate string
    
    SET @StartYr = 2003
    SET @EndYr = 2006
    
    for(int year=@StartYr; year<EndYr; year++) 
    {
      //append months
      @StartDate = '01/01/' + CASTYEARTOSTRING(year)
      @EndDate = '12/31/' + CASTYEARTOSTRING(year)
    
      Select
        *
      From
        Table1
      Where Table1.dateCompleted BETWEEN @StartDate and @EndDate
    }
    TIA!

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Why not simply run the SELECT between 01/01/2003 and 12/31/2006 instead of iterating through thm?

    For this specific question the best loop construct to use would probably be a Do While loop:
    Code:
    Do While StartYr <= EndYr
      Debug.Print StartYr
      StartYr = StartYr + 1
    Loop
    George
    Home | Blog

  3. #3
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •