+ Reply to Thread
Results 1 to 8 of 8

SQL Date Parameters

  1. #1
    Registered User
    Join Date
    10-19-2010
    Location
    Norwich
    MS-Off Ver
    Excel 2002 SP3
    Posts
    4

    SQL Date Parameters

    Hi there,

    I am trying to create a simple query to extract data from an SQL 2008 R2 database. The only problem I am having is with the syntax for inserting dates into the SQL string. When I run the code I do not get any errors but it also returns no results when it should. It's probably worth mentioning it's not a problem with UK/US date formats too because I've tried it using dates that work both ways. Here's my code:

    Please Login or Register  to view this content.
    Any ideas why that SQL statement isn't working? If I take out the WHERE clause it's fine.

    Thanks,

    Matt
    Last edited by Gyto; 10-20-2010 at 04:21 AM.

  2. #2
    Registered User
    Join Date
    10-19-2010
    Location
    Norwich
    MS-Off Ver
    Excel 2002 SP3
    Posts
    4

    Re: SQL Date Parameters

    Still can't find a solution to this :o( Any ideas? Is there a certain syntax I should be using? I've tried inserting #'s around the date and allsorts...!

    Thanks!

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: SQL Date Parameters

    Have you tried formatting DateFrom and DateTo to Strings appropriate to SQLServer date format ?

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-19-2010
    Location
    Norwich
    MS-Off Ver
    Excel 2002 SP3
    Posts
    4

    Re: SQL Date Parameters

    You may be on to something there....I just checked the SQL View that was created for me and the date fields are varchar for some reason. I tried editing the SQL statement as shown below to try and correct this but still no luck so far...not sure if I can do it this way?!

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: SQL Date Parameters

    I'm not an expert but dabble in vba. Try the below.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: SQL Date Parameters

    I don't have a SQL Server install to test against but my experience with MySQL would tell me you need to parse the dates as I outlined (ie as strings in appropriate format)

    edit: I am not saying the above with any authority I should add....
    Last edited by DonkeyOte; 10-20-2010 at 05:53 AM.

  7. #7
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: SQL Date Parameters

    Sorry didnt see your post DonkeyOte, looks like I just duplicated your solution but in a different way.

  8. #8
    Registered User
    Join Date
    10-19-2010
    Location
    Norwich
    MS-Off Ver
    Excel 2002 SP3
    Posts
    4

    Re: SQL Date Parameters

    All sorted guys - I tried the suggestions with the date formats but with no luck, so I got the developer at their end to alter the SQL View so the dates were actually date fields instead of varchar fields and it started working immediately. I guess I'll never know if it was possible the other way...! I can't see why it wouldn't work converting them at query runtime but oh well!

    Thanks you your help, much appreciated!

+ 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