+ Reply to Thread
Results 1 to 4 of 4

SQL Strings, Looping and Using Date Values

  1. #1
    Registered User
    Join Date
    09-26-2003
    Posts
    17

    Arrow SQL Strings, Looping and Using Date Values

    I am looking for some advice on passing a range of dates to a SQL string that I have.

    I want to run my macro and obtain the date part of my SQL string from a cell on another worksheet, currently I have the following line in to achieve this;

    SQL_RTSids = SQL_RTSids & "'" & Worksheets("DateRange").Range("A2") & " 06:00:00' "

    QUESTION 1 : How can I pass a date to my SQL string (I am currently passing a text string ie ’14-NOV-10 and then adding the time of 0600hrs onto the date within the SQL string.
    I also have a list of dates that I want to pass through my SQL.

    Given my limited knowledge of creating a SQL string I presume I would need to;
    1. Create my string using the first date (worksheet ‘DateRange”, cell “A2”) from the other worksheet and paste it from a record set into my active worksheet
    2. Run the SQL again but this time use the next date (worksheet ‘DateRange”, cell “A3”) on the other worksheet and then paste in underneath the previous set of data
    3. Repeat the above until the date in the cell is less than sysdate

    QUESTION 2 : How can I loop though the data on my other worksheet and include them in my SQL string, stopping when the date is less than sysdate?

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Strings, Looping and Using Date Values

    It's difficult to say without a sample workbook, to pass a date in SQL you need to enclose in #'s. It should also be in the US date format "mm/dd/yyyy".

    I'm not clear on whether you are wanting to pass a range of dates in one query, or loop through multiple queries, one for each date.

    Also you are currently adding "06:00:00" manually, so I'm guessing you don't have this in the date cell?

    I think what you are looking for is something like the below:

    Please Login or Register  to view this content.
    Let me know if this isn't what you are looking for

  3. #3
    Registered User
    Join Date
    09-26-2003
    Posts
    17

    Re: SQL Strings, Looping and Using Date Values

    Thanks for your help.

    I managed to get around QUESTION 1 using a TEXT(AA2,"DD-MM-YY HH:MM:SS") formula to convert my date into a text string.

    QUESTION 2 is still causing me problems.

    I have my date range in cells AB2:AB100 ..... my code is below, how can I loop the SQL to get the section of code .....

    & Worksheets("DateRange").Range("AB2") &

    .... to get the next date each time it runs (ie. 1st time get date in AB2 and paste code in worksheet (I have the code to paste, it is just the loop that is proiving challneging), 2nd time use date in AB3, 3rd time use date in AB4 etc etc etc until I get to AB100.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Wink Re: SQL Strings, Looping and Using Date Values

    I think you want something like below, though I'm still not sure what you are looking for. Are you wanting to run the query multiple times, one for each date? Or are you wanting to pass all your dates and return everything, I've included both options below.

    N.B I have followed your lead and passed the dates in SQL as strings, not dates - this depends on the data type in your database, but you should really use dates rather than strings.

    Also you say that you are using the following to convert to text:
    Please Login or Register  to view this content.
    In SQL you should pass the dates as "mm/dd/yy" not "dd/mm/yyyy". Though if as you suggest, you have the dates stored in your database as a string, this shouldn't really matter, also I found it better to change the dates to strings in VBA rather than an Excel fromula (using Format) as it reduces the amount of steps and makes troubleshooting a bit easier.


    This will move down the range each time the sub is called:
    Please Login or Register  to view this content.
    This will loop through and run a separate query for each date in the range:
    Please Login or Register  to view this content.
    This will loop through all the dates in the range A2:A100 and add them into your query in a single call:
    Please Login or Register  to view this content.
    I haven't tested, this, so look out for typos! Does this answer your question? If not let me know.
    Last edited by Kyle123; 12-03-2010 at 05:22 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1