mysql: between not working with date











up vote
2
down vote

favorite












I am Writing a Between query with formatted date..
This is my query:






SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')






However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.



If anyone can find anything out of this, please tell me..



One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.










share|improve this question


















  • 1




    You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
    – Paul Spiegel
    Apr 20 '16 at 20:15










  • I am using the same format to compare results there is no issue in it. What dates you have in the table column
    – shzyincu
    Apr 20 '16 at 20:44















up vote
2
down vote

favorite












I am Writing a Between query with formatted date..
This is my query:






SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')






However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.



If anyone can find anything out of this, please tell me..



One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.










share|improve this question


















  • 1




    You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
    – Paul Spiegel
    Apr 20 '16 at 20:15










  • I am using the same format to compare results there is no issue in it. What dates you have in the table column
    – shzyincu
    Apr 20 '16 at 20:44













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am Writing a Between query with formatted date..
This is my query:






SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')






However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.



If anyone can find anything out of this, please tell me..



One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.










share|improve this question













I am Writing a Between query with formatted date..
This is my query:






SELECT shop_id, date_format(registered_time,'%d-%m-%Y') as Date FROM
shops where (date_format(registered_time,'%d-%m-%Y') BETWEEN
'09-03-2016' AND '19-04-2016')






However, when I execute query, it gives my only the records between date 09 and 19 regardless of month.
For example, I have records like 30-03-2016, 31-03-2016..but they are ignored.



If anyone can find anything out of this, please tell me..



One more thing is that, I am converting this date from time stamp field. I hope that isn't causing any issues.







mysql date between






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 20 '16 at 19:36









Archit

747




747








  • 1




    You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
    – Paul Spiegel
    Apr 20 '16 at 20:15










  • I am using the same format to compare results there is no issue in it. What dates you have in the table column
    – shzyincu
    Apr 20 '16 at 20:44














  • 1




    You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
    – Paul Spiegel
    Apr 20 '16 at 20:15










  • I am using the same format to compare results there is no issue in it. What dates you have in the table column
    – shzyincu
    Apr 20 '16 at 20:44








1




1




You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15




You are comparing strings not dates. And '30..' is not between '09..' and '19..'.
– Paul Spiegel
Apr 20 '16 at 20:15












I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44




I am using the same format to compare results there is no issue in it. What dates you have in the table column
– shzyincu
Apr 20 '16 at 20:44












3 Answers
3






active

oldest

votes

















up vote
2
down vote













You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).



You need to use real DATE values for BETWEEN to work properly.





  • If the columns are already DATE columns, just skip the formatting:



    SELECT shop_id, registered_time FROM shops where registered_time 
    BETWEEN '2016-03-09' AND '2016-04-19'



If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.






share|improve this answer






























    up vote
    1
    down vote













    You need to turn your strings back into dates that MySQL understands. From the documentation:



    CAST(datetime_col AS DATE)


    should help accomplish what you want.



    Reference






    share|improve this answer

















    • 1




      CAST() is unlikely to understand the DD-MM-YYYY format though.
      – Pekka 웃
      Apr 20 '16 at 19:42


















    up vote
    -1
    down vote













    Do this:



    select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');


    I had the same issue, then I tried this and it worked. Good Luck!






    share|improve this answer










    New contributor




    M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Please replace '2018-11-10' with your desired date!
      – M IMRAN FARUQI
      yesterday











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36753530%2fmysql-between-not-working-with-date%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).



    You need to use real DATE values for BETWEEN to work properly.





    • If the columns are already DATE columns, just skip the formatting:



      SELECT shop_id, registered_time FROM shops where registered_time 
      BETWEEN '2016-03-09' AND '2016-04-19'



    If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.






    share|improve this answer



























      up vote
      2
      down vote













      You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).



      You need to use real DATE values for BETWEEN to work properly.





      • If the columns are already DATE columns, just skip the formatting:



        SELECT shop_id, registered_time FROM shops where registered_time 
        BETWEEN '2016-03-09' AND '2016-04-19'



      If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).



        You need to use real DATE values for BETWEEN to work properly.





        • If the columns are already DATE columns, just skip the formatting:



          SELECT shop_id, registered_time FROM shops where registered_time 
          BETWEEN '2016-03-09' AND '2016-04-19'



        If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.






        share|improve this answer














        You can't take two arbitrary strings (like 30-03-2016) and expect BETWEEN to behave like it would for real DATE columns (a behaviour that is hard-coded into mySQL).



        You need to use real DATE values for BETWEEN to work properly.





        • If the columns are already DATE columns, just skip the formatting:



          SELECT shop_id, registered_time FROM shops where registered_time 
          BETWEEN '2016-03-09' AND '2016-04-19'



        If your existing columns are in the DD-MM-YYYY format, convert them to dates using STR_TO_DATE() - either on the fly just for the purposes of this query (sloooowwwww!) or permanently.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 11:54









        Community

        11




        11










        answered Apr 20 '16 at 19:40









        Pekka 웃

        352k1138361009




        352k1138361009
























            up vote
            1
            down vote













            You need to turn your strings back into dates that MySQL understands. From the documentation:



            CAST(datetime_col AS DATE)


            should help accomplish what you want.



            Reference






            share|improve this answer

















            • 1




              CAST() is unlikely to understand the DD-MM-YYYY format though.
              – Pekka 웃
              Apr 20 '16 at 19:42















            up vote
            1
            down vote













            You need to turn your strings back into dates that MySQL understands. From the documentation:



            CAST(datetime_col AS DATE)


            should help accomplish what you want.



            Reference






            share|improve this answer

















            • 1




              CAST() is unlikely to understand the DD-MM-YYYY format though.
              – Pekka 웃
              Apr 20 '16 at 19:42













            up vote
            1
            down vote










            up vote
            1
            down vote









            You need to turn your strings back into dates that MySQL understands. From the documentation:



            CAST(datetime_col AS DATE)


            should help accomplish what you want.



            Reference






            share|improve this answer












            You need to turn your strings back into dates that MySQL understands. From the documentation:



            CAST(datetime_col AS DATE)


            should help accomplish what you want.



            Reference







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 20 '16 at 19:41









            Jacob See

            715617




            715617








            • 1




              CAST() is unlikely to understand the DD-MM-YYYY format though.
              – Pekka 웃
              Apr 20 '16 at 19:42














            • 1




              CAST() is unlikely to understand the DD-MM-YYYY format though.
              – Pekka 웃
              Apr 20 '16 at 19:42








            1




            1




            CAST() is unlikely to understand the DD-MM-YYYY format though.
            – Pekka 웃
            Apr 20 '16 at 19:42




            CAST() is unlikely to understand the DD-MM-YYYY format though.
            – Pekka 웃
            Apr 20 '16 at 19:42










            up vote
            -1
            down vote













            Do this:



            select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');


            I had the same issue, then I tried this and it worked. Good Luck!






            share|improve this answer










            New contributor




            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Please replace '2018-11-10' with your desired date!
              – M IMRAN FARUQI
              yesterday















            up vote
            -1
            down vote













            Do this:



            select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');


            I had the same issue, then I tried this and it worked. Good Luck!






            share|improve this answer










            New contributor




            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Please replace '2018-11-10' with your desired date!
              – M IMRAN FARUQI
              yesterday













            up vote
            -1
            down vote










            up vote
            -1
            down vote









            Do this:



            select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');


            I had the same issue, then I tried this and it worked. Good Luck!






            share|improve this answer










            New contributor




            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            Do this:



            select col_one,col_two from table_name where date_col between date('2018-11-10') AND date('2018-11-10');


            I had the same issue, then I tried this and it worked. Good Luck!







            share|improve this answer










            New contributor




            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited yesterday









            Marcel Stör

            14k447122




            14k447122






            New contributor




            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered yesterday









            M IMRAN FARUQI

            11




            11




            New contributor




            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            M IMRAN FARUQI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • Please replace '2018-11-10' with your desired date!
              – M IMRAN FARUQI
              yesterday


















            • Please replace '2018-11-10' with your desired date!
              – M IMRAN FARUQI
              yesterday
















            Please replace '2018-11-10' with your desired date!
            – M IMRAN FARUQI
            yesterday




            Please replace '2018-11-10' with your desired date!
            – M IMRAN FARUQI
            yesterday


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36753530%2fmysql-between-not-working-with-date%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Coverage of Google Street View

            Full-time equivalent

            Surfing