Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sql Query Required!

Sql Query Required!

Scheduled Pinned Locked Moved Database
databasehelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • 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.
  • Z Offline
    Z Offline
    Zishan Haider
    wrote on last edited by
    #1

    I have a column with type datetime in an employee's table. I want to delete the records that older than 3 working days. The problem is I can't use getdate()-3 in where clause because I don't know about the holidays and all.. Can anyone help me out ? Regards, Zishan

    C 1 Reply Last reply
    0
    • Z Zishan Haider

      I have a column with type datetime in an employee's table. I want to delete the records that older than 3 working days. The problem is I can't use getdate()-3 in where clause because I don't know about the holidays and all.. Can anyone help me out ? Regards, Zishan

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Zishan Haider wrote: The problem is I can't use getdate()-3 in where clause because I don't know about the holidays and all.. Can anyone help me out ? You will need information on the holidays if you are to achieve this.


      My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      Z 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Zishan Haider wrote: The problem is I can't use getdate()-3 in where clause because I don't know about the holidays and all.. Can anyone help me out ? You will need information on the holidays if you are to achieve this.


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        Z Offline
        Z Offline
        Zishan Haider
        wrote on last edited by
        #3

        Can't I use some kind of loop in the SQL :confused:

        C 2 Replies Last reply
        0
        • Z Zishan Haider

          Can't I use some kind of loop in the SQL :confused:

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          You can use loops in SQL but they are very inefficient. However, you've already said that you don't know the holiday information so I don't see how a loop is going to be useful.


          My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

          1 Reply Last reply
          0
          • Z Zishan Haider

            Can't I use some kind of loop in the SQL :confused:

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Assuming you have a table called Holidays with the a column containing the date of each holiday you could write a query like this:

            DECLARE @days int;
            SELECT @days = COUNT(*)+3 FROM Holidays
            WHERE HolidayDate <= getdate() AND HolidayDate >= getdate()-3

            You can then use the @days variable to count back the number of days. For example:

            DELETE FROM MyTable WHERE somedate < getdate-@days

            Does this help?


            My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

            Z 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Assuming you have a table called Holidays with the a column containing the date of each holiday you could write a query like this:

              DECLARE @days int;
              SELECT @days = COUNT(*)+3 FROM Holidays
              WHERE HolidayDate <= getdate() AND HolidayDate >= getdate()-3

              You can then use the @days variable to count back the number of days. For example:

              DELETE FROM MyTable WHERE somedate < getdate-@days

              Does this help?


              My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

              Z Offline
              Z Offline
              Zishan Haider
              wrote on last edited by
              #6

              Well.. you're right couldn't do it in SQL without a holidays table. However I did it in my C# program. I selected distinct dates from table ordered by Date Desc. Then I deleted every record with the date from my select query ignoring the first three.. :-O Yes I know its poor performance, but this chunk has to run once every morning so I don't need any major performance here. And I had to do it without A holidays table. Thanks for your help :) Regards Zishan

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups