Solved

Diff Date

Posted on 2008-06-25
2
973 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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