Results 1 to 9 of 9

Thread: Need help on my SQL statement with date criteria

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    65

    Need help on my SQL statement with date criteria

    I've this appl using VB.Net 2005 as front-end and MS Access as my DB. I have a table with a date field defined using the date format as "short date'. When look at it under MS Access, the data shows data in mm/dd/yyyy format (e.g. 12/23/2006"

    I tried the following SQA statement to retrieve a set of records based on the following sample date range:

    First attempt:
    SELECT * FROM [tblWork_Order] WHERE WO_Creation_Date >= '12-15-2006' AND WO_Creation_Date <= '2-6-2007'

    Then tried
    SELECT * FROM [tblWork_Order] WHERE WO_Creation_Date >= '12/15/2006' AND WO_Creation_Date <= '2/6/2007'


    None of them work, I got an error message "Datatype mismatched in criteria expression.

    Can someone please tells me where is my problem ?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Need help on my SQL statement with date criteria

    Access?? Access uses # to set off dates....

    SELECT * FROM [tblWork_Order] WHERE WO_Creation_Date >= #12-15-2006# AND WO_Creation_Date <= #2-6-2007#

    Also, you may want to consider a less anbiguous date format, like yyyy-mm-dd .....
    SELECT * FROM [tblWork_Order] WHERE WO_Creation_Date >= #2006-12-15# AND WO_Creation_Date <= #2007-2-6#

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Addicted Member Daystar's Avatar
    Join Date
    Dec 2006
    Location
    Pahrump, NV
    Posts
    132

    Re: Need help on my SQL statement with date criteria

    Quote Originally Posted by techgnome
    Access?? Access uses # to set off dates....

    SELECT * FROM [tblWork_Order] WHERE WO_Creation_Date >= #12-15-2006# AND WO_Creation_Date <= #2-6-2007#

    Also, you may want to consider a less anbiguous date format, like yyyy-mm-dd .....
    SELECT * FROM [tblWork_Order] WHERE WO_Creation_Date >= #2006-12-15# AND WO_Creation_Date <= #2007-2-6#

    -tg
    In access it is best to use Between

    Code:
    Select * from [tblWork_Order] Where WO_Creation_Date Between #12/15/2006# and #02/06/2007#

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help on my SQL statement with date criteria

    There's no such thing as an ambiguous date format in SQL. SQL only understands U.S. format, i.e. MM/dd/yyyy. ALL date literals in SQL code MUST be in that format, although single digit months can have a leading zero or not.

    Also, just because Access only DISPLAYS the date portion doesn't mean that the field doesn't CONTAIN a time portion too. If you have saved your values with a time portion then the database fields will contain that time as well as the date.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member Daystar's Avatar
    Join Date
    Dec 2006
    Location
    Pahrump, NV
    Posts
    132

    Re: Need help on my SQL statement with date criteria

    jmchilney, Access can display the time portion too.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help on my SQL statement with date criteria

    Quote Originally Posted by Daystar
    jmchilney, Access can display the time portion too.
    I didn't say that it couldn't. What I said was that your setting the properties of a column to display only the date portion doesn't mean that it doesn't have a time portion.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help on my SQL statement with date criteria

    Quote Originally Posted by jmcilhinney
    There's no such thing as an ambiguous date format in SQL. SQL only understands U.S. format, i.e. MM/dd/yyyy. ALL date literals in SQL code MUST be in that format, although single digit months can have a leading zero or not.
    I'm afraid I'll have to technically disagree there.. 12/15/2006 and 15/12/2006 both work (and have identical values) for the majority of DBMS's. You will however get problems if you try to use 07/02/2007 to mean the 7th of Feb (as it will be interpreted as 2nd of July, no matter what your PC/database/.. settings are).

    The format that TG suggested (yyyy-mm-dd) is just as valid as MM/dd/yyyy, I believe it is also part of the SQL89/92 standard for dates. The difference is that if you forgot to format the date, the cause of problems is clearer.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help on my SQL statement with date criteria

    I bow to superior knowledge. Sorry for doubting you tg.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Need help on my SQL statement with date criteria

    a thousand lashing with a wet noodle for you.... and don't let it happen again.

    As it is, it's only been in the last few weeks that I've been using the yyyy-mm-dd format as I've been dealing with some truly whacked formats (where EVERYTHING, and I mean EVERYTHING was sent as text).


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width