Results 1 to 7 of 7

Thread: date format mismatch

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    date format mismatch

    Hi,

    I'm using access and my date field is formatted as shortdate - dd/mm/yyyy.
    But when I try to query the table as below, there is a datatype mismatch.


    Code:
    SELECT *
    FROM tablename
    WHERE datefield='12/04/2007';
    is it possible to format the date field in the sql statement or should I format the date value in the front end of the application and then pass this.

    I would appreciate any help. Thanks in advance

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: date format mismatch

    That is because Access does not support single quotes for dates.

    In Access, you need to enclose dates with the # sign.

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

    Re: date format mismatch

    For Access, Dates need to be surrounded by # instead of '

    Much more importantly, dates in an SQL string will always be interpreted as MM/DD/YYYY unless they are blatantly meant to be different (such as 31/04/2007).

    When putting dates into SQL strings you should format them to either MM/DD/YYYY or (better) YYYY/MM/DD

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: date format mismatch

    Well I tried using hash as below


    Code:
    SELECT *
    FROM tablename
    WHERE datefield=#12/11/2007#;
    but no data was returned in Access although the table had data for 12/11/2007. in the table though the value is displayed in dd/mm/yyyy format when i click on that field it becomes 12/11/2007 7:58:10 AM.

    I'm passing this as a paramter from my front end in C#, since it kept throwing a datattype mismatch error 'm trying to make it work inside access.

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

    Re: date format mismatch

    It doesn't matter what the date format is in the table design (that is only used for Access forms etc), in SQL statements you must use the formats I suggested.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: date format mismatch

    Hmmm...ok.....but why isn't the query returning any data despite having matches in the table?? I'm using the format u suggested.
    Last edited by sunshine123; Apr 13th, 2007 at 12:10 PM.

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

    Re: date format mismatch

    In that case I would assume that there is also a time in the field. If that is the case, the field will not be exactly the value you specified.

    One way to solve it is to use Between with the time for the end of the day, eg:
    Code:
    WHERE date1 Between #04/13/2007# AND #04/13/2007 23:59:59#

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