简体   繁体   English

使用存储过程将星期一计算为一周的第一天

[英]Calculate Monday as first day of the week with a stored procedure

I have this C# code to pull up the number of postings created within the last week, which is working fine. 我有这个C#代码来增加上周内创建的发布数量,效果很好。 But it's pulling up count from "last 7 days"; 但这要从“过去7天”开始增加; what I want to do is to set the first day of the week to "Monday" so that when we pull up the count of posting for "week", it should pull up the number from "monday to sunday" instead of default "last 7 days". 我想做的是将一周的第一天设置为“星期一”,这样当我们增加“星期”的发布次数时,它应该将数字从“星期一”增加到“星期日”,而不是默认的“ last” 7天”。

I created a class to set Monday as the 1st day of the week, but what I want to know is how can I (if I can) encapsulate that method in this code to simply pull count of posting etc 我创建了一个类,将星期一设置为一周的第一天,但​​是我想知道的是,如何(如果可以)将该方法封装在此代码中,以简单地获取发布等信息

Here is my code: 这是我的代码:

if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
    lblJobPostings.Text = ds.Tables[0].Rows[0]["new_job_posting_this_week"].ToString();
}
if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
{
    lblNewEmployers.Text = ds.Tables[1].Rows[0]["new_employer_this_week"].ToString();
}
if (ds.Tables[2] != null && ds.Tables[2].Rows.Count > 0)
{
    lblNewInstitutes.Text = ds.Tables[2].Rows[0]["new_institutes_this_week"].ToString();
}

public static DateTime CallFirstDayOfWeek(DateTime input)
{
   int Delta = (7 - (DayOfWeek.Monday - input.DayOfWeek)) % 7;
   return input.AddDays(-Delta);
}

Stored procedure: 存储过程:

select COUNT(od.id) [new_job_posting_this_week] 
from rs_job_posting od
where od.date_created = GETDATE()-7

Try this in your Stored Procedure: 在您的存储过程中尝试以下操作:

DECLARE @CurrentWeekday AS INT
DECLARE @LastSunday AS DATETIME
DECLARE @LastMonday AS DATETIME

SET @CurrentWeekday = DATEPART(WEEKDAY, GETDATE())
// Count backwards from today to get to the most recent Sunday.
// (@CurrentWeekday % 7) - 1 will give the number of days since Sunday; 
// -1 negates for subtraction.
SET @LastSunday = DATEADD(DAY, -1 * (( @CurrentWeekday % 7) - 1), GETDATE())
// Monday is obviously one day after last Sunday.
SET @LastMonday = DATEADD(DAY, 1, @LastSunday)

SELECT COUNT(od.id) [new_job_posting_this_week] 
FROM rs_job_posting od
WHERE od.date_created >= @LastMonday
select COUNT(od.id) [new_job_posting_this_week] 
from rs_job_posting od
where od.date_created Between Convert(DateTime, DATEADD(D,-7,GETDATE())) And Convert(DateTime, GetDate())

To select 7 days starting on say last Monday, iterate through the last 7 days in C# until you hit Monday, then pass that date into the SQL Proc. 要选择从上周一说的7天开始,请在C#中迭代最后7天,直到您达到周一为止,然后将该日期传递到SQL Proc中。

for (int i = 0; i < 7; i++)
            {
                DateTime mydate = DateTime.Now.AddDays(-1);
                if (mydate.DayOfWeek == DayOfWeek.Monday)
                {
                    //call sp here
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM