Mixing LIKE and BETWEEN

Discussion in 'MySQL' started by timallard, Sep 1, 2009.

  1. #1
    Hi all,

    Im using PHP and MySQL to do a date range search..
    Can I do a date range search to compare to dates using LIKE and BETWEEN?

    e.x.

    My Date Format: 01/01/2009 4:00pm

    WHERE DateStamp BETWEEN '% 4:00pm' AND '% 5:00pm'";

    My syntax is wrong and need a little help...please let me know what I am doing wrong, thank you.
     
    timallard, Sep 1, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can either use REGEXP:

    WHERE datestamp REGEXP '4:[0-9][0-9][ab]m'
    Code (markup):
    Or date_format

    WHERE date_format(datestamp, '%h') = '4'
    Code (markup):
    Both untested. It's also possible that for the first query you'll have to use date_format first to convert to string.
     
    premiumscripts, Sep 1, 2009 IP
  3. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #3
    This is beautiful!! Thank You!

    I used the first example. now..if I wanted to have it within a range...that is where I am still stuck..

    e.x. between 4:00 - 5:00
     
    timallard, Sep 1, 2009 IP
  4. aquilax

    aquilax Member

    Messages:
    126
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #4
    Use
    WHERE DATE_FORMAT(date,'%H') >= 16 AND DATE_FORMAT(date,'%H') <= 17
     
    aquilax, Sep 1, 2009 IP
    timallard likes this.
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah, I was confused by your question as between 4:00 and 5:00 meant to me a range from 4:00 to 4:59, so that's why I only did = '4'. Aquilax' solution should work. If you want to use the regexp solution you can just do

    WHERE datestamp REGEXP '[45]:[0-9][0-9][ab]m'
     
    premiumscripts, Sep 1, 2009 IP
  6. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #6
    You all rock so hard. Rep to both of you! Thank you!
     
    timallard, Sep 1, 2009 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    1 last question,... i promise...

    it is duplicating results for 1:00 and 11:00 any way around this? I am looking up reg expressions but cant seem to find my solution. Thanks!!
     
    timallard, Sep 1, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    WHERE datestamp REGEXP '0[15]:[0-9][0-9][ab]m' -> for hours 01 and 05

    Or:

    WHERE datestamp REGEXP '(01|05):[0-9][0-9][ab]m'
     
    premiumscripts, Sep 2, 2009 IP
  9. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #9
    WONDERFUL. Wow you are amazing! I am studying to be like you seriously. But I keep getting stuck with unique instances...

    LAST REGEXP question.

    I have a time frame of 11:**pm, 12:**am, 1:**am, 2:**am i need to pull -
    so, essentially the first one is AM and the rest of the times are PM..I am stuck, any ideas?

    Thank you!!!!
     
    timallard, Sep 2, 2009 IP
  10. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Haha, no problem, just trying to help. Try this regex:

    WHERE datestamp REGEXP '(11:[0-9]{2}pm|(12|01|02):[0-9]{2}am)'

    Btw, if you want a nice online regex tester, try http://regexpal.com/ -- it will highlight what each line that it matches. Quite nice.
     
    premiumscripts, Sep 2, 2009 IP
  11. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #11
    This is great. I am learning so much. It's funny actually, how once I see the solution, it just clicks. Like - wow, that totally makes sense now.

    Thanks for the help, resources and learning.
    BUY FROM PREMIUM SCRIPTS! ++
     
    timallard, Sep 2, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Well, all I can say is it's refreshing to see someone who's actually learning from this stuff instead of just mindlessly copy/pasting the solution :)
     
    premiumscripts, Sep 2, 2009 IP