Solved

Diff Date

Posted on 2008-06-25
2
980 Views
Last Modified: 2008-08-26
I want to get TotalDay not to include Weekend.


DATEDIFF(d, myorderdate, sentdate) AS TotalDay  ??? This give me total includes Weekend


"select accountnumber as ACCOUNT, convert(char(10), myorderdate, 101) as Orderdate, convert(char(10), duedate, 101) as Duedate, Sentddate as ClosedDate, DATEDIFF(d, myorderdate, sentdate) AS TotalDay  from mytable...
0
Comment
Question by:VBdotnet2005
2 Comments
 
LVL 14

Expert Comment

by:raja_ind82
ID: 21869895
Use this function to get the date difference, excluding Weekends (Saturday & sunday)

Function DiffWeekdays(ByVal datDay1 As Date, ByVal datDay2 As Date) As Long
        ' Comments  : Returns the number of weekdays between two dates
        '             The days are rounded down -- it takes 24 hours to make a day.
        ' Parameters: datDay1 - first (earlier) date/time (subtracted from datDay2)
        '             datDay2 - second (later) date/time
        ' Returns   : Number of whole business days between two dates
        '             (Returns negative days if datDay1 is after datDay2)
        '
        Dim lngDays As Long
        Dim lngWeeks As Long
        Dim datFirstDate As Date
        Dim datLastDate As Date
        Dim datNewDate As Date
        Dim intDirection As Integer

        If datDay1 < datDay2 Then
            datFirstDate = datDay1
            datLastDate = datDay2
            intDirection = 1
        Else
            datFirstDate = datDay2
            datLastDate = datDay1
            intDirection = -1
        End If

        ' Subtract weekends
        lngWeeks = ((Convert.ToInt32(datLastDate.DayOfWeek) - Convert.ToInt32(datFirstDate.DayOfWeek)) / 7)
        lngDays = lngWeeks * 5
        datNewDate = DateAdd(DateInterval.Day, (lngWeeks * 7), datFirstDate)
        While datNewDate < datLastDate
            datNewDate = DateAdd(DateInterval.Day, 1, datNewDate)
            If datNewDate <= datLastDate Then
                ' Don't count days when new day is Sunday or Saturday.
                ' (When new day is Saturday, you are actually counting Friday)
                If Weekday(datNewDate) <> 1 And Weekday(datNewDate) <> 7 Then
                    lngDays = lngDays + 1
                    MsgBox(Weekday(datNewDate) & " " & lngDays)
                End If
            End If
        End While
        DiffWeekdays = intDirection * lngDays
    End Function

Regards,
M.Raja
0
 

Accepted Solution

by:
VBdotnet2005 earned 0 total points
ID: 21880354
DATEDIFF(d, myorderdate, sentdate) -2*DATEDIFF(weeked, myorderdate, sentdate) as Totalday is the answer. It works for me. Please refund  
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question