Date range SQL querie not working?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #1

    Tech Date range SQL querie not working?

    This is what I'm using which, from what I have seen online, should be working:

    "SELECT * FROM CookingShows WHERE Date BETWEEN '2022-01-09' AND Date '2022-01-10' ORDER BY Date DESC;");
    Multiple sites are saying that is the correct format, yet it isnt displaying any data that I have in the table.

    Any thoughts?
    Extreme Link List - v1.0
  • sarettah
    see you later, I'm gone
    • Oct 2002
    • 14297

    #2
    Originally posted by Publisher Bucks
    This is what I'm using which, from what I have seen online, should be working:


    "SELECT * FROM CookingShows WHERE Date BETWEEN '2022-01-09' AND Date '2022-01-10' ORDER BY Date DESC;");

    Multiple sites are saying that is the correct format, yet it isnt displaying any data that I have in the table.

    Any thoughts?
    1. You have the syntax wrong. The correct syntax is "where date between date1 and date2" not "where date between date1 and date date2".

    2. You are trying to compare a string to a date. You need to cast the strings as dates if you want a date compare. "where date between cast(date1 as date) and cast(date2 as date)"

    3. Date is a keyword in MYSQL and should not be used as a variable name or field name. So where you have your column called date ("where Date...") should be something like "where order_date...." Or whatever other name the date is stored as.


    .
    All cookies cleared!

    Comment

    • Publisher Bucks
      Confirmed User
      • Oct 2018
      • 1330

      #3
      Originally posted by sarettah
      [b]2. You are trying to compare a string to a date. You need to cast the strings as dates if you want a date compare. "where date between cast(date1 as date) and cast(date2 as date)"]/b]

      .
      Thanks.

      I renamed the column to showdate and also redid the syntax, which is strange as StackOverFlow had tons of people saying that worked as I posted lol

      Can you explain that part above for me a little? I'm not quite sure what you mean.
      Extreme Link List - v1.0

      Comment

      • redwhiteandblue
        Bollocks
        • Jun 2007
        • 2793

        #4
        MySQL (and PHP for that matter) does not magically recognize '2022-01-10' as a date. It's just a string with some numbers and hyphens in it. You can compare two strings but it's not the same as comparing dates. You have to tell it it's a date by casting it from "string" type to "date" type.
        Interserver unmanaged AMD Ryzen servers from $73.00

        Comment

        • Klen
          • Aug 2006
          • 32235

          #5
          Sample of code which work for me:

          PHP Code:
          SELECT SUM(`P`) AS 'M' FROM `C` WHERE `DateTime` between '$yy-$mm-$dd 00:00:00' AND '$yy-$mm-$dd 23:59:59' 
          

          Comment

          • blackmonsters
            Making PHP work
            • Nov 2002
            • 20961

            #6
            Edit : already posted above by Sarettah




            Free Open Source Live Aggregated Cams Script (FOSLACS)

            Comment

            • Publisher Bucks
              Confirmed User
              • Oct 2018
              • 1330

              #7
              So this is what I have now:

              $result = mysqli_query($con,"SELECT * FROM CookingShows WHERE ShowDate (date as DATE) BETWEEN CAST('2022-09-01' AS DATE) AND CAST('2022-10-01' AS DATE)");
              It still doesnt show me any data.

              The column is set to varchar(255) if that makes any difference? It isnt set as date, datetime, or anything like that, should it be?
              Extreme Link List - v1.0

              Comment

              • Publisher Bucks
                Confirmed User
                • Oct 2018
                • 1330

                #8
                Originally posted by Klen
                Sample of code which work for me:

                PHP Code:
                SELECT SUM(`P`) AS 'M' FROM `C` WHERE `DateTime` between '$yy-$mm-$dd 00:00:00' AND '$yy-$mm-$dd 23:59:59' 
                
                Thanks will give that a try now
                Extreme Link List - v1.0

                Comment

                • sarettah
                  see you later, I'm gone
                  • Oct 2002
                  • 14297

                  #9
                  WHERE ShowDate (date as DATE)
                  Where did you get that from?

                  It should be "where ShowDate between......"

                  Unless you are storing a string in the database instead of a date. In that case it would be

                  "where cast(ShowDate as date) between....

                  .
                  All cookies cleared!

                  Comment

                  • Publisher Bucks
                    Confirmed User
                    • Oct 2018
                    • 1330

                    #10
                    Originally posted by sarettah
                    Where did you get that from?

                    It should be "where ShowDate between......"

                    Unless you are storing a string in the database instead of a date. In that case it would be

                    "where cast(ShowDate as date) between....

                    .
                    I threw it together lol

                    Thank you
                    Extreme Link List - v1.0

                    Comment

                    • k0nr4d
                      Confirmed User
                      • Aug 2006
                      • 9231

                      #11
                      Originally posted by redwhiteandblue
                      MySQL (and PHP for that matter) does not magically recognize '2022-01-10' as a date. It's just a string with some numbers and hyphens in it. You can compare two strings but it's not the same as comparing dates. You have to tell it it's a date by casting it from "string" type to "date" type.
                      Mysql actually does - you don't have to cast it as a date. BETWEEN '2022-01-01' AND '2022-12-31' will work fine. It's possible it might not work if you have strict mode on but overall it does parse it by itself.
                      Mechanical Bunny Media
                      Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                      Comment

                      • redwhiteandblue
                        Bollocks
                        • Jun 2007
                        • 2793

                        #12
                        Originally posted by k0nr4d
                        Mysql actually does - you don't have to cast it as a date. BETWEEN '2022-01-01' AND '2022-12-31' will work fine. It's possible it might not work if you have strict mode on but overall it does parse it by itself.
                        It will work but technically it's because the string comparison works with the date in that format. MySQL doesn't know those strings contain dates until you do something date related with them like assigning them to a date column.
                        Interserver unmanaged AMD Ryzen servers from $73.00

                        Comment

                        • Nitzer Ebb
                          Confirmed User
                          • Apr 2015
                          • 797

                          #13
                          Originally posted by Publisher Bucks
                          This is what I'm using which, from what I have seen online, should be working:



                          Multiple sites are saying that is the correct format, yet it isnt displaying any data that I have in the table.

                          Any thoughts?
                          remove date after AND
                          -= Krampus Productions =-

                          Comment

                          • Publisher Bucks
                            Confirmed User
                            • Oct 2018
                            • 1330

                            #14
                            Thanks everyone, I have this working now
                            Extreme Link List - v1.0

                            Comment

                            • k0nr4d
                              Confirmed User
                              • Aug 2006
                              • 9231

                              #15
                              Originally posted by redwhiteandblue
                              It will work but technically it's because the string comparison works with the date in that format. MySQL doesn't know those strings contain dates until you do something date related with them like assigning them to a date column.
                              Well I mean yeah, I suppose you are right in that respect - it works if you use a DATE or TIMESTAMP field but I suppose it might not work without casting if you have it in a VARCHAR or something.
                              Mechanical Bunny Media
                              Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                              Comment

                              Working...