Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I need a Column where condition is Target_L *X / Y

where I need x and y should come automatically from query.
Y=Total Number of working days in month ,Excluding Sunday  i.e for Jan, 27 days are  working days.
X=Working days till yesterday's date i.e 23(Minus Sunday, 1 to 26 = 26-3=23)
   today is 27 january 2018 

final =<pre>Target_L
* 23/27

------------------
There is a second approach also
X= Total working days i.e 31 for January Including Sunday
Y=till date working days Including Sunday
26/31

From both Any approach is Good for me,I need q query for this ..I have date column,Month column.
Using SQL SERVER

What I have tried:

new to sql server date approach
Posted
Updated 30-Jan-18 3:10am
v2
Comments
OriginalGriff 27-Jan-18 3:34am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is in your table, what values you need, or what needs to be calculated.
So show us sample input and output data, explain why it is the way it is, and show us what you have tried so far - we can't help you from a vague "I need this" description.

Use the "Improve question" widget to edit your question and provide better information.
Member 13518187 27-Jan-18 6:17am    
Question is clear enough
CHill60 28-Jan-18 14:47pm    
Obviously your question is NOT clear enough or someone would have answered it. Try following OriginalGriff's advice.
By the way...use the Reply link when replying to a comment

1 solution

You need a function that return the number of working days between two date. but i suggest a CLR that it's faster for that type of computation.


  [SqlFunction(SystemDataAccess = SystemDataAccessKind.Read)]
    public static SqlInt32 NETWORKDAYS(SqlDateTime start, SqlDateTime end, object weekend, SqlChars holidays)
    {
     
        if (start.IsNull || end.IsNull) return SqlInt32.Null;
        int sign = start <= end ? 1 : -1;
        if (start > end)
        {
            SqlDateTime tmp = start;
            start = end;
            end = tmp;
        }
        int stage1 = end.Value.Date.Subtract(start.Value.Date).Days + 1;
        int d1 = (int) GetStartWeekOffsetforMonday(start.Value.DayOfWeek);
        int d2 = (int)GetStartWeekOffsetforMonday(end.Value.DayOfWeek);
        int weekendcount;
        bool[] isweekend = GetWeekends(weekend, out weekendcount, "NETWORKDAYS");
        int remnant = (stage1 - 1) % 7;
        int stage2 = stage1 - weekendcount * (stage1 - remnant) / 7;
        for (int i = d1; i <= d1 + remnant; i++) if (isweekend[i % 7]) stage2--;
        if (holidays.IsNull) return stage2 * sign;
        List<DateTime> hols = GetHolidays(holidays, "NETWORKDAYS");
        foreach(DateTime hday in hols)
            if (!isweekend[ GetStartWeekOffsetforMonday(hday.DayOfWeek)] && hday >= start.Value.Date && hday <= end.Value.Date) stage2--;
        return stage2 * sign;
    }

private static int GetStartWeekOffsetforMonday(DayOfWeek Day)
    {

        switch (Day)
        {
            case DayOfWeek.Monday: return 0;
               
            case   DayOfWeek.Tuesday: return 1;
            case DayOfWeek.Wednesday: return 2;
            case DayOfWeek.Thursday:  return 3;
            case DayOfWeek.Friday:    return 4;
             case   DayOfWeek.Saturday:  return 5;
            case   DayOfWeek.Sunday  :    return 6;
        }
        return 0;
    }
 
Share this answer
 
Comments
Member 13518187 30-Jan-18 10:03am    
@Luca Astolfi where should i write this CLR...only have sql server database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900