Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to fetch Data Between Two Dates ?

How to fetch Data Between Two Dates ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Ramkumar MohanR Offline
    Ramkumar MohanR Offline
    Ramkumar Mohan
    wrote on last edited by
    #1

    I wrote this code on how to fetch data between two dates ( FROM date to To date).?

    QDate fdate= ui->dateEdit_4->date();
    QString fdateString = fdate.toString("dd / MM / yyyy");
    QDate tdate= ui->dateEdit_5->date();
    QString tdateString = tdate.toString("dd / MM / yyyy");
    QSqlQueryModel * modal = new QSqlQueryModel();
    QSqlQuery* query=new QSqlQuery(mydb);
    query->prepare("SELECT PID , TName , ANrmlRang , BNrmlRang , ODVal , ResultOD , Date , Time FROM Reports WHERE Date BETWEEN '"+fdateString+"' AND '"+tdateString+"'");

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • Ramkumar MohanR Ramkumar Mohan

      I wrote this code on how to fetch data between two dates ( FROM date to To date).?

      QDate fdate= ui->dateEdit_4->date();
      QString fdateString = fdate.toString("dd / MM / yyyy");
      QDate tdate= ui->dateEdit_5->date();
      QString tdateString = tdate.toString("dd / MM / yyyy");
      QSqlQueryModel * modal = new QSqlQueryModel();
      QSqlQuery* query=new QSqlQuery(mydb);
      query->prepare("SELECT PID , TName , ANrmlRang , BNrmlRang , ODVal , ResultOD , Date , Time FROM Reports WHERE Date BETWEEN '"+fdateString+"' AND '"+tdateString+"'");

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ramkumar-Mohan
      And? Please actually ask a question! What happened? Did you get an error? Did it not behave as you expected? In what way, what did you see or not see?

      What do you actually expect to happen from this code as shown...?

      Ramkumar MohanR 1 Reply Last reply
      0
      • ali-aydinA Offline
        ali-aydinA Offline
        ali-aydin
        wrote on last edited by
        #3

        Hi,
        You must have date field in your data and when you are querying you can
        fetch data by "where" expression if you are using SQL

        JonBJ 1 Reply Last reply
        0
        • ali-aydinA ali-aydin

          Hi,
          You must have date field in your data and when you are querying you can
          fetch data by "where" expression if you are using SQL

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @ali-aydin
          The OP shows a Date field, and uses it in a WHERE clause already. I don't think that in itself is any issue.

          1 Reply Last reply
          0
          • Ramkumar MohanR Ramkumar Mohan

            I wrote this code on how to fetch data between two dates ( FROM date to To date).?

            QDate fdate= ui->dateEdit_4->date();
            QString fdateString = fdate.toString("dd / MM / yyyy");
            QDate tdate= ui->dateEdit_5->date();
            QString tdateString = tdate.toString("dd / MM / yyyy");
            QSqlQueryModel * modal = new QSqlQueryModel();
            QSqlQuery* query=new QSqlQuery(mydb);
            query->prepare("SELECT PID , TName , ANrmlRang , BNrmlRang , ODVal , ResultOD , Date , Time FROM Reports WHERE Date BETWEEN '"+fdateString+"' AND '"+tdateString+"'");

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Ramkumar-Mohan
            couple of thing to note, first,

            you're supposed to bindValue() to bind to placeholders/values in your query. Construction the query in place is error prone and unreadable.

            • AFAIK the query is supposed to end on a ;not sure if it's mandatory or not.
            • dd / MM / yyyy is not the default format for dates in SQL, that would be YYYY-MM-DD are you sure your format is correct, and not simply 2 strings that are compared ?
            • if the type of your db is actually DATE , than use QDate that one is actually mapped to DATE. https://doc.qt.io/qt-6/sql-types.html
            • when you execute the query and it returns false, what does https://doc.qt.io/qt-6/qsqlquery.html#lastError actually tell you ?

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            JonBJ 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @Ramkumar-Mohan
              couple of thing to note, first,

              you're supposed to bindValue() to bind to placeholders/values in your query. Construction the query in place is error prone and unreadable.

              • AFAIK the query is supposed to end on a ;not sure if it's mandatory or not.
              • dd / MM / yyyy is not the default format for dates in SQL, that would be YYYY-MM-DD are you sure your format is correct, and not simply 2 strings that are compared ?
              • if the type of your db is actually DATE , than use QDate that one is actually mapped to DATE. https://doc.qt.io/qt-6/sql-types.html
              • when you execute the query and it returns false, what does https://doc.qt.io/qt-6/qsqlquery.html#lastError actually tell you ?
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @J-Hilk said in How to fetch Data Between Two Dates ?:

              when you execute the query and it returns false

              But that was my point: what does the user expect, it shows a prepare() and no exec() so nothing is going to happen....

              1 Reply Last reply
              0
              • JonBJ JonB

                @Ramkumar-Mohan
                And? Please actually ask a question! What happened? Did you get an error? Did it not behave as you expected? In what way, what did you see or not see?

                What do you actually expect to happen from this code as shown...?

                Ramkumar MohanR Offline
                Ramkumar MohanR Offline
                Ramkumar Mohan
                wrote on last edited by Ramkumar Mohan
                #7

                @JonB

                d.JPG

                As shown in this photo, if I check (From date to To date), I want to find the data within those days. I wrote the above code and it didn't work for me.

                JonBJ 1 Reply Last reply
                0
                • Ramkumar MohanR Ramkumar Mohan

                  @JonB

                  d.JPG

                  As shown in this photo, if I check (From date to To date), I want to find the data within those days. I wrote the above code and it didn't work for me.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Ramkumar-Mohan

                  I wrote the above code and it didn't work for me.

                  The above code does not execute any SQL query. So in itself it won't do anything, like I said. I don't know whether you have other code to execute it, because you don't say; if you do, why not say so, we cannot guess?

                  For the rest start with @J-Hilk's comments.

                  Ramkumar MohanR 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Ramkumar-Mohan

                    I wrote the above code and it didn't work for me.

                    The above code does not execute any SQL query. So in itself it won't do anything, like I said. I don't know whether you have other code to execute it, because you don't say; if you do, why not say so, we cannot guess?

                    For the rest start with @J-Hilk's comments.

                    Ramkumar MohanR Offline
                    Ramkumar MohanR Offline
                    Ramkumar Mohan
                    wrote on last edited by
                    #9

                    @JonB

                    Sorry sir, I shared my code, please find the Below Attachment

                    da.JPG

                    JonBJ 1 Reply Last reply
                    0
                    • Ramkumar MohanR Ramkumar Mohan

                      @JonB

                      Sorry sir, I shared my code, please find the Below Attachment

                      da.JPG

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Ramkumar-Mohan
                      OK, at least now I know. You might be surprised --- we have indeed had to people who prepare queries but never execute them!

                      You don't check the return result from the exec(). You don't tell me whether it returns any rows. If I am to guess, are you saying "it does return rows, but it returns too many rows, it returns rows outside of my desired date range"? That is what I need to hear!

                      So like I said start with @J-Hilk's observations. Above all, how do we know whether

                      BETWEEN '03 / 03 / 2022' AND  '05 / 06 / 2022'
                      

                      is even acceptable to your SQL implementation?

                      Print out exactly what the query string is. Try it in whatever command-line tool you have for sending queries to your database. Try it with a plain Date = ... instead of BETWEEN to test the format if you wonder whether BETWEEN works. And/or use a bound variable to pass a QDate value instead of trying to construct a literal string for it.

                      Ramkumar MohanR 2 Replies Last reply
                      2
                      • JonBJ JonB

                        @Ramkumar-Mohan
                        OK, at least now I know. You might be surprised --- we have indeed had to people who prepare queries but never execute them!

                        You don't check the return result from the exec(). You don't tell me whether it returns any rows. If I am to guess, are you saying "it does return rows, but it returns too many rows, it returns rows outside of my desired date range"? That is what I need to hear!

                        So like I said start with @J-Hilk's observations. Above all, how do we know whether

                        BETWEEN '03 / 03 / 2022' AND  '05 / 06 / 2022'
                        

                        is even acceptable to your SQL implementation?

                        Print out exactly what the query string is. Try it in whatever command-line tool you have for sending queries to your database. Try it with a plain Date = ... instead of BETWEEN to test the format if you wonder whether BETWEEN works. And/or use a bound variable to pass a QDate value instead of trying to construct a literal string for it.

                        Ramkumar MohanR Offline
                        Ramkumar MohanR Offline
                        Ramkumar Mohan
                        wrote on last edited by
                        #11

                        @JonB Ok

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Ramkumar-Mohan
                          OK, at least now I know. You might be surprised --- we have indeed had to people who prepare queries but never execute them!

                          You don't check the return result from the exec(). You don't tell me whether it returns any rows. If I am to guess, are you saying "it does return rows, but it returns too many rows, it returns rows outside of my desired date range"? That is what I need to hear!

                          So like I said start with @J-Hilk's observations. Above all, how do we know whether

                          BETWEEN '03 / 03 / 2022' AND  '05 / 06 / 2022'
                          

                          is even acceptable to your SQL implementation?

                          Print out exactly what the query string is. Try it in whatever command-line tool you have for sending queries to your database. Try it with a plain Date = ... instead of BETWEEN to test the format if you wonder whether BETWEEN works. And/or use a bound variable to pass a QDate value instead of trying to construct a literal string for it.

                          Ramkumar MohanR Offline
                          Ramkumar MohanR Offline
                          Ramkumar Mohan
                          wrote on last edited by
                          #12

                          @JonB

                          1.JPG

                          Now , this code is working fine sir, Thank you.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved