SQL WHERE clause to pick data for last 7 days











up vote
0
down vote

favorite












I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?



Original creation_date field is in varchar like '18/11/08' and I use the CONVERT(datetime, creation_date,11) to convert it into 2018-11-08 00:00:00.000, but I don't know how to do in the WHERE clause, so it only selects all records created for last 7 days.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?



    Original creation_date field is in varchar like '18/11/08' and I use the CONVERT(datetime, creation_date,11) to convert it into 2018-11-08 00:00:00.000, but I don't know how to do in the WHERE clause, so it only selects all records created for last 7 days.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?



      Original creation_date field is in varchar like '18/11/08' and I use the CONVERT(datetime, creation_date,11) to convert it into 2018-11-08 00:00:00.000, but I don't know how to do in the WHERE clause, so it only selects all records created for last 7 days.










      share|improve this question















      I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?



      Original creation_date field is in varchar like '18/11/08' and I use the CONVERT(datetime, creation_date,11) to convert it into 2018-11-08 00:00:00.000, but I don't know how to do in the WHERE clause, so it only selects all records created for last 7 days.







      sql tsql sql-server-2012






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 18:04









      Zohar Peled

      51.8k73172




      51.8k73172










      asked Nov 8 at 18:00









      Mapperkids Lee

      32111




      32111
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          use where clause like below



           select t.* from your_table t
          where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())





          share|improve this answer























          • You are missing a ) at the end of your query...
            – Zohar Peled
            Nov 8 at 18:08










          • @ZoharPeled thanks edited
            – Zaynul Abadin Tuhin
            Nov 8 at 18:09










          • Yes, thanks and it works!
            – Mapperkids Lee
            Nov 8 at 18:24


















          up vote
          1
          down vote













          In order to get the best performance, you should keep the calculation away from the column:



          SELECT * 
          FROM <YourTable>
          WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)


          This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats






          share|improve this answer






























            up vote
            0
            down vote













            The best thing to do is store dates properly to begin with - in a column with a Date data type.

            Assuming you can't change the database structure, you can use DateDiff with GetDate():



            SELECT <ColumnsList>
            FROM <TableName>
            WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7


            Of course, you need to replace the <ColumnsList> with the list of columns and <TableName> with the actual table name.






            share|improve this answer





















            • Making a calculation on a column like this can cause horrible performance
              – t-clausen.dk
              Nov 8 at 18:35










            • @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
              – Zohar Peled
              Nov 8 at 18:38












            • no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
              – t-clausen.dk
              Nov 8 at 18:41












            • I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
              – Zohar Peled
              Nov 8 at 18:44










            • yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
              – t-clausen.dk
              Nov 8 at 18:47











            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%2f53213609%2fsql-where-clause-to-pick-data-for-last-7-days%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            use where clause like below



             select t.* from your_table t
            where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())





            share|improve this answer























            • You are missing a ) at the end of your query...
              – Zohar Peled
              Nov 8 at 18:08










            • @ZoharPeled thanks edited
              – Zaynul Abadin Tuhin
              Nov 8 at 18:09










            • Yes, thanks and it works!
              – Mapperkids Lee
              Nov 8 at 18:24















            up vote
            1
            down vote



            accepted










            use where clause like below



             select t.* from your_table t
            where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())





            share|improve this answer























            • You are missing a ) at the end of your query...
              – Zohar Peled
              Nov 8 at 18:08










            • @ZoharPeled thanks edited
              – Zaynul Abadin Tuhin
              Nov 8 at 18:09










            • Yes, thanks and it works!
              – Mapperkids Lee
              Nov 8 at 18:24













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            use where clause like below



             select t.* from your_table t
            where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())





            share|improve this answer














            use where clause like below



             select t.* from your_table t
            where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 18:09

























            answered Nov 8 at 18:03









            Zaynul Abadin Tuhin

            11k2831




            11k2831












            • You are missing a ) at the end of your query...
              – Zohar Peled
              Nov 8 at 18:08










            • @ZoharPeled thanks edited
              – Zaynul Abadin Tuhin
              Nov 8 at 18:09










            • Yes, thanks and it works!
              – Mapperkids Lee
              Nov 8 at 18:24


















            • You are missing a ) at the end of your query...
              – Zohar Peled
              Nov 8 at 18:08










            • @ZoharPeled thanks edited
              – Zaynul Abadin Tuhin
              Nov 8 at 18:09










            • Yes, thanks and it works!
              – Mapperkids Lee
              Nov 8 at 18:24
















            You are missing a ) at the end of your query...
            – Zohar Peled
            Nov 8 at 18:08




            You are missing a ) at the end of your query...
            – Zohar Peled
            Nov 8 at 18:08












            @ZoharPeled thanks edited
            – Zaynul Abadin Tuhin
            Nov 8 at 18:09




            @ZoharPeled thanks edited
            – Zaynul Abadin Tuhin
            Nov 8 at 18:09












            Yes, thanks and it works!
            – Mapperkids Lee
            Nov 8 at 18:24




            Yes, thanks and it works!
            – Mapperkids Lee
            Nov 8 at 18:24












            up vote
            1
            down vote













            In order to get the best performance, you should keep the calculation away from the column:



            SELECT * 
            FROM <YourTable>
            WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)


            This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats






            share|improve this answer



























              up vote
              1
              down vote













              In order to get the best performance, you should keep the calculation away from the column:



              SELECT * 
              FROM <YourTable>
              WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)


              This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                In order to get the best performance, you should keep the calculation away from the column:



                SELECT * 
                FROM <YourTable>
                WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)


                This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats






                share|improve this answer














                In order to get the best performance, you should keep the calculation away from the column:



                SELECT * 
                FROM <YourTable>
                WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)


                This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 19:01

























                answered Nov 8 at 18:30









                t-clausen.dk

                35.6k104279




                35.6k104279






















                    up vote
                    0
                    down vote













                    The best thing to do is store dates properly to begin with - in a column with a Date data type.

                    Assuming you can't change the database structure, you can use DateDiff with GetDate():



                    SELECT <ColumnsList>
                    FROM <TableName>
                    WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7


                    Of course, you need to replace the <ColumnsList> with the list of columns and <TableName> with the actual table name.






                    share|improve this answer





















                    • Making a calculation on a column like this can cause horrible performance
                      – t-clausen.dk
                      Nov 8 at 18:35










                    • @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
                      – Zohar Peled
                      Nov 8 at 18:38












                    • no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
                      – t-clausen.dk
                      Nov 8 at 18:41












                    • I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
                      – Zohar Peled
                      Nov 8 at 18:44










                    • yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
                      – t-clausen.dk
                      Nov 8 at 18:47















                    up vote
                    0
                    down vote













                    The best thing to do is store dates properly to begin with - in a column with a Date data type.

                    Assuming you can't change the database structure, you can use DateDiff with GetDate():



                    SELECT <ColumnsList>
                    FROM <TableName>
                    WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7


                    Of course, you need to replace the <ColumnsList> with the list of columns and <TableName> with the actual table name.






                    share|improve this answer





















                    • Making a calculation on a column like this can cause horrible performance
                      – t-clausen.dk
                      Nov 8 at 18:35










                    • @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
                      – Zohar Peled
                      Nov 8 at 18:38












                    • no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
                      – t-clausen.dk
                      Nov 8 at 18:41












                    • I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
                      – Zohar Peled
                      Nov 8 at 18:44










                    • yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
                      – t-clausen.dk
                      Nov 8 at 18:47













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    The best thing to do is store dates properly to begin with - in a column with a Date data type.

                    Assuming you can't change the database structure, you can use DateDiff with GetDate():



                    SELECT <ColumnsList>
                    FROM <TableName>
                    WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7


                    Of course, you need to replace the <ColumnsList> with the list of columns and <TableName> with the actual table name.






                    share|improve this answer












                    The best thing to do is store dates properly to begin with - in a column with a Date data type.

                    Assuming you can't change the database structure, you can use DateDiff with GetDate():



                    SELECT <ColumnsList>
                    FROM <TableName>
                    WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7


                    Of course, you need to replace the <ColumnsList> with the list of columns and <TableName> with the actual table name.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 at 18:03









                    Zohar Peled

                    51.8k73172




                    51.8k73172












                    • Making a calculation on a column like this can cause horrible performance
                      – t-clausen.dk
                      Nov 8 at 18:35










                    • @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
                      – Zohar Peled
                      Nov 8 at 18:38












                    • no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
                      – t-clausen.dk
                      Nov 8 at 18:41












                    • I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
                      – Zohar Peled
                      Nov 8 at 18:44










                    • yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
                      – t-clausen.dk
                      Nov 8 at 18:47


















                    • Making a calculation on a column like this can cause horrible performance
                      – t-clausen.dk
                      Nov 8 at 18:35










                    • @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
                      – Zohar Peled
                      Nov 8 at 18:38












                    • no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
                      – t-clausen.dk
                      Nov 8 at 18:41












                    • I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
                      – Zohar Peled
                      Nov 8 at 18:44










                    • yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
                      – t-clausen.dk
                      Nov 8 at 18:47
















                    Making a calculation on a column like this can cause horrible performance
                    – t-clausen.dk
                    Nov 8 at 18:35




                    Making a calculation on a column like this can cause horrible performance
                    – t-clausen.dk
                    Nov 8 at 18:35












                    @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
                    – Zohar Peled
                    Nov 8 at 18:38






                    @t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
                    – Zohar Peled
                    Nov 8 at 18:38














                    no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
                    – t-clausen.dk
                    Nov 8 at 18:41






                    no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
                    – t-clausen.dk
                    Nov 8 at 18:41














                    I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
                    – Zohar Peled
                    Nov 8 at 18:44




                    I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a Date data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
                    – Zohar Peled
                    Nov 8 at 18:44












                    yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
                    – t-clausen.dk
                    Nov 8 at 18:47




                    yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
                    – t-clausen.dk
                    Nov 8 at 18:47


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213609%2fsql-where-clause-to-pick-data-for-last-7-days%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini