Solved

Diff Date

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now