Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Posts
    10

    Unanswered: DateTime Query Question

    Hi all,

    I wrote an SQL sentence but it generates "Syntax error (missing operator) in query expression 'MyDate=20.04.2004 10:25:22'" error message.

    What is the error on this SQL sentence?

    "SELECT * FROM SomeTable WHERE MyDate=" + DateTime.Now + ""

    Thanks for any reply...

    Note: DateTime.Now is a C# code which returns the current date in 20.04.2004 10:24:56 format.

  2. #2
    Join Date
    Jun 2003
    Location
    USA
    Posts
    1,032
    Remember to put # before and after a date field variable, ' before and after a text field variable, and nothing before and after a number field variable.
    J. Paul Schmidt, Freelance Web and Database Developer
    www.Bullschmidt.com
    Access Database Sample, Web Database Sample, ASP Design Tips

  3. #3
    Join Date
    Jul 2003
    Posts
    10
    Firstly, thanks for your reply. I placed # before and after my date filed and now I got the following error:

    "Syntax error in date in query expression 'MyDate=#20.04.2004 13:22:46#'"

    My new SQL sentence is now this:
    "SELECT * FROM SomeTable WHERE MyDate=#" + DateTime.Now + "#"

    What is the problem in this SQL sentence?

  4. #4
    Join Date
    Jun 2003
    Location
    USA
    Posts
    1,032
    Perhaps try something like this which just worked for me with different table and field names:

    "SELECT * FROM SomeTable WHERE MyDate=Date();"

    Or

    "SELECT * FROM SomeTable WHERE MyDate=Now();"

    But of course the latter would only find a record with the same date/time as the current second. So may want to use min and max dates if really storing a date/time instead of just a date.
    J. Paul Schmidt, Freelance Web and Database Developer
    www.Bullschmidt.com
    Access Database Sample, Web Database Sample, ASP Design Tips

  5. #5
    Join Date
    Jun 2003
    Location
    USA
    Posts
    1,032
    And of course be sure that your date/time field is really a date/time field and not a number field or text field.
    J. Paul Schmidt, Freelance Web and Database Developer
    www.Bullschmidt.com
    Access Database Sample, Web Database Sample, ASP Design Tips

Posting Permissions

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