Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want a loop that searches from day 1 to 30/31/28 of a month date is the first day of current month dw returns 1 for sunday so if dw is a sunday then count it that's the logic;
I am facing 2 prbs
1)dw here returns 4 for sunday in Vstudio (may be Int 32 is a prb i guess) however in Sqlserver Datepart returns one for the same date.
2) Date add sets date to 2nd of month in 2nd ittration bt after that it fails


C#
int firstday =Convert.ToInt32( Helper.ExeCuteReader("SELECT Day( CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(getdate())-1),getdate()),101))"));// first day of month
                    // till the day in which you r giving salary
                 //int lastday =Convert.ToInt32( Helper.ExeCuteReader("SELECT Day( CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,getdate()))),DATEADD(mm,1,getdate())),101) )"));//last day
                 int today = Convert.ToInt32(Helper.ExeCuteReader("Select Day(getdate())"));
                 int totaldays = Convert.ToInt32(Helper.ExeCuteReader("SELECT DateDiff(dd,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(getdate())-1),getdate()),101),getdate())"));
                 DateTime date = Convert.ToDateTime(Helper.ExeCuteReader("SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(getdate())-1),getdate()),101) "));
                 // day1--datetime value
                 int dw = 0,sunday=0;
                 for (int i = firstday; i <= today; i++)
                 {
                     string a = Helper.ExeCuteReader("select datepart(dw, '" + date + "')");
                     dw = Convert.ToInt32(a);
                     // day1--datetime value
                     string c = Helper.ExeCuteReader("select DATEADD(day,1,'" + date + "')");
                     date = Convert.ToDateTime(c);
                     if (dw == 1)//it should be one bt int32 is conerting it to four
                     {
                         sunday++;
                     }
Posted
Updated 11-Jun-13 19:44pm
v3
Comments
CHill60 11-Jun-13 10:54am    
Firstly drop the "txt-speak" - it makes you look childish and tends to put people off helping.
When you debug what is being returned in a and what is date set to?
Matt T Heffron 11-Jun-13 14:12pm    
Why are you doing all of this in SQL?
Your SQL doesn't appear to be actually referencing a DB!
This would be very doable directly using the DateTime and TimeSpan types, and almost certainly much more efficient.
Member 9956178 12-Jun-13 1:33am    
I want a loop that searches from day 1 to 30/31/28 of a month
date is the first day of current month
dw returns 1 for sunday
so if dw is a sunday then count it
that's the logic
Its working well only in frist and second itration, moreover dw returns 4 for sunday in Vstudio (may be Int 32 is a prb i guess)

1 solution

I second Matt T Heffron. Do it in code instead of database.

Since I didn't understand your requirement very well, here's an example of working with DateTime[^] coded at my whim:
C#
void ShowExample()
{
    DateTime salaryDay = DateTime.Now;

    DateTime monthStart = new DateTime(salaryDay.Year, salaryDay.Month, 1);

    int daysInMonth = DateTime.DaysInMonth(salaryDay.Year, salaryDay.Month);
    DateTime monthEnd = new DateTime(salaryDay.Year, salaryDay.Month, daysInMonth);

    int sundays = 0;
    for (DateTime dt = monthStart; dt < monthEnd; dt += TimeSpan.FromDays(1))
    {
        if (dt.DayOfWeek == DayOfWeek.Sunday)
        {
            sundays++;
        }
    }
}
 
Share this answer
 
Comments
damodara naidu betha 13-Jun-13 3:40am    
Good one Lukeer. 5+ .

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