between 2 dates

Discussion in 'MySQL' started by onlyican.com, Dec 21, 2005.

  1. #1
    Hey

    I have set up a db and i want to make a query between 2 dates, the dates format is for example today (21-12-2005)

    I used this

    SELECT * FROM cust WHERE '23-01-2005'> sold_date AND '20-01-2006' <= sold_date;


    but it only picks up the first 2 numbers, and compares that.

    my data consists of 21-12-05 and 22-12-05

    using that brings everything up, ok

    but change the first date from 23-01-2005 to 20-01-2005 and nothing comes up

    21-12-05 comes after 20-01-05 doesn't it????
     
    onlyican.com, Dec 21, 2005 IP
  2. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Date formats are usually database dependent. Mysql likes to use: 2005-12-31 while Oracle might use: '21-Mar-05'.

    Make sure you use the right format for your database or use a function to convert the date to something your database likes.
     
    tandac, Dec 21, 2005 IP
  3. Integrov

    Integrov Guest

    Messages:
    79
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    in mysql you should use date_diff
     
    Integrov, Dec 31, 2005 IP
  4. Postingpays

    Postingpays Well-Known Member

    Messages:
    1,071
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    185
    #4
    When we are making queries using dates we have to be careful. We cant use single quoataion for this. Use this query, hope it will solve your problem

    SELECT * FROM cust WHERE '23-01-2005'> sold_date AND '20-01-2006' <= sold_date;

    SELECT * FROM cust WHERE sold_date between #23-01-2005# AND #20-01-2006#
     
    Postingpays, Aug 15, 2006 IP