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. Number of weekdays between to dates

Number of weekdays between to dates

Scheduled Pinned Locked Moved Database
9 Posts 3 Posters 2 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.
  • S Offline
    S Offline
    szukuro
    wrote on last edited by
    #1

    I need to calculate the number of weekdays between two dates in SQL. Does someone know an easy way for that?

    H 1 Reply Last reply
    0
    • S szukuro

      I need to calculate the number of weekdays between two dates in SQL. Does someone know an easy way for that?

      H Offline
      H Offline
      Harini N K
      wrote on last edited by
      #2

      Hi Do you mean number of weeks between between two dates ?? Then use DATEDIFF() function Eg: SELECT DATEDIFF (ww, '01/01/2007',getdate() will 10 weeks :)

      Harini

      S 1 Reply Last reply
      0
      • H Harini N K

        Hi Do you mean number of weeks between between two dates ?? Then use DATEDIFF() function Eg: SELECT DATEDIFF (ww, '01/01/2007',getdate() will 10 weeks :)

        Harini

        S Offline
        S Offline
        szukuro
        wrote on last edited by
        #3

        No, I meant the number of weekdays. For '01/01/2007' and getdate() it should return 51 (as of today). So to be more precise I need something like this in SQL:

            DateTime start = new DateTime(2007, 1, 1);
            int num = 0;
            while (DateTime.Compare(DateTime.Today, start) == 1)
            {
                int day = (int)start.DayOfWeek; 
                if (day > 0 && day < 6)
                    num++;
                start = start.AddDays(1);
            }
        

        -- modified at 10:47 Tuesday 13th March, 2007

        S H A 3 Replies Last reply
        0
        • S szukuro

          No, I meant the number of weekdays. For '01/01/2007' and getdate() it should return 51 (as of today). So to be more precise I need something like this in SQL:

              DateTime start = new DateTime(2007, 1, 1);
              int num = 0;
              while (DateTime.Compare(DateTime.Today, start) == 1)
              {
                  int day = (int)start.DayOfWeek; 
                  if (day > 0 && day < 6)
                      num++;
                  start = start.AddDays(1);
              }
          

          -- modified at 10:47 Tuesday 13th March, 2007

          S Offline
          S Offline
          szukuro
          wrote on last edited by
          #4

          I feel a bit silly now as I noticed I basically answered my own question. All I had to do is rewrite the sample provided above in SQL, using the same logic. :doh:

          1 Reply Last reply
          0
          • S szukuro

            No, I meant the number of weekdays. For '01/01/2007' and getdate() it should return 51 (as of today). So to be more precise I need something like this in SQL:

                DateTime start = new DateTime(2007, 1, 1);
                int num = 0;
                while (DateTime.Compare(DateTime.Today, start) == 1)
                {
                    int day = (int)start.DayOfWeek; 
                    if (day > 0 && day < 6)
                        num++;
                    start = start.AddDays(1);
                }
            

            -- modified at 10:47 Tuesday 13th March, 2007

            H Offline
            H Offline
            Harini N K
            wrote on last edited by
            #5

            Hi You can also try this:

            SELECT DATEDIFF (dd, '01/01/2007',getdate()) - (DATEDIFF (wk, '01/01/2007',getdate()) * 2)

            Harini

            S 1 Reply Last reply
            0
            • H Harini N K

              Hi You can also try this:

              SELECT DATEDIFF (dd, '01/01/2007',getdate()) - (DATEDIFF (wk, '01/01/2007',getdate()) * 2)

              Harini

              S Offline
              S Offline
              szukuro
              wrote on last edited by
              #6

              SELECT DATEDIFF (dd, '03/11/2007', '03/17/2007') - (DATEDIFF (wk, '03/11/2007','03/17/2007') * 2) returns 6 and that's wrong.

              H 1 Reply Last reply
              0
              • S szukuro

                SELECT DATEDIFF (dd, '03/11/2007', '03/17/2007') - (DATEDIFF (wk, '03/11/2007','03/17/2007') * 2) returns 6 and that's wrong.

                H Offline
                H Offline
                Harini N K
                wrote on last edited by
                #7

                Hi Check this link: SQL Forums[^] Copied and pasted example from the above link:

                CREATE function dbo.DateDiffWeekdays
                (@fromdate datetime,
                @todate datetime)
                RETURNS int
                AS
                begin
                declare @procdate datetime, @enddate datetime
                declare @weekdays int
                set @procdate = @fromdate
                set @weekdays = 0

                while (@procdate < @todate)
                begin
                if (datepart(dw, @procdate + 1) <> 1) and (datepart(dw, @procdate + 1) <> 7)
                set @weekdays = @weekdays + 1
                set @procdate = dateadd(d, 1, @procdate)

                end

                if @todate is null
                set @weekdays = null

                return @weekdays
                end

                -- modified at 5:52 Wednesday 14th March, 2007 But still this is wrong, if you give between Mar 1st 2007 and March 3rd 2007 :(

                Harini

                S 1 Reply Last reply
                0
                • H Harini N K

                  Hi Check this link: SQL Forums[^] Copied and pasted example from the above link:

                  CREATE function dbo.DateDiffWeekdays
                  (@fromdate datetime,
                  @todate datetime)
                  RETURNS int
                  AS
                  begin
                  declare @procdate datetime, @enddate datetime
                  declare @weekdays int
                  set @procdate = @fromdate
                  set @weekdays = 0

                  while (@procdate < @todate)
                  begin
                  if (datepart(dw, @procdate + 1) <> 1) and (datepart(dw, @procdate + 1) <> 7)
                  set @weekdays = @weekdays + 1
                  set @procdate = dateadd(d, 1, @procdate)

                  end

                  if @todate is null
                  set @weekdays = null

                  return @weekdays
                  end

                  -- modified at 5:52 Wednesday 14th March, 2007 But still this is wrong, if you give between Mar 1st 2007 and March 3rd 2007 :(

                  Harini

                  S Offline
                  S Offline
                  szukuro
                  wrote on last edited by
                  #8

                  Actually that's very similiar to what I came up with rewriting my own code in SQL. Mine's:

                  CREATE FUNCTION WeekDaysBetween
                  (
                  @from datetime,
                  @to datetime
                  )
                  AS
                  BEGIN
                  DECLARE @num int
                  SET @num = 0
                  WHILE (DATEDIFF(day, @from, @to) > 0)
                  BEGIN
                  DECLARE @day int
                  SET @day = DATEPART(dw, @from)
                  IF (@day > 1 AND @day < 7)
                  SET @num = @num + 1
                  SET @from = DATEADD(day,1 , @from)
                  END
                  RETURN @num
                  END

                  This seems to work. Thanks for your help though.

                  1 Reply Last reply
                  0
                  • S szukuro

                    No, I meant the number of weekdays. For '01/01/2007' and getdate() it should return 51 (as of today). So to be more precise I need something like this in SQL:

                        DateTime start = new DateTime(2007, 1, 1);
                        int num = 0;
                        while (DateTime.Compare(DateTime.Today, start) == 1)
                        {
                            int day = (int)start.DayOfWeek; 
                            if (day > 0 && day < 6)
                                num++;
                            start = start.AddDays(1);
                        }
                    

                    -- modified at 10:47 Tuesday 13th March, 2007

                    A Offline
                    A Offline
                    Avatto
                    wrote on last edited by
                    #9

                    Hi, You can use SELECT with the same method you used to get the above results. Please find this site http://www.avatto.com/ which is good for any database related queries... :)

                    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