SQL。 SP 或函数应计算周五的下一个日期

发布于 2024-11-07 02:07:34 字数 146 浏览 0 评论 0原文

我需要编写一个存储过程来返回给定日期的下周五日期?例如 - 如果日期是 05/12/2011,那么它应该返回下周五日期 05/13/2011。如果您通过 05/16/2011,那么它应该返回日期是 5/20/2011(星期五)。如果您将星期五作为日期,那么它应该返回相同的日期。

I need to write a store procedure that will return a next friday date on a given date? for example - if the date is 05/12/2011, then it should return next friday date as 05/13/2011. If you pass, 05/16/2011, then it should return the date is 5/20/2011 (Friday). If you pass friday as the date, then it should return the same date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟燃烟灭 2024-11-14 02:07:34

我会将其设为标量 UDF,因为它更容易使用输出。

CREATE FUNCTION dbo.GetNextFriday(
@D DATETIME
)
RETURNS DATETIME 
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN DATEADD(DAY,(13 - (@@DATEFIRST + DATEPART(WEEKDAY,@D)))%7,@D)
END

I'd make this a scalar UDF as it is easier to consume the output.

CREATE FUNCTION dbo.GetNextFriday(
@D DATETIME
)
RETURNS DATETIME 
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN DATEADD(DAY,(13 - (@@DATEFIRST + DATEPART(WEEKDAY,@D)))%7,@D)
END
梦罢 2024-11-14 02:07:34

这是针对 SQL Server 2008 的。要在 2005 中使用,只需将日期字段更改为您的日期时间到日期转换的首选项。它还假设您没有更改默认的周开始值。

DECLARE @PassedDate date = '5/21/2011';
SELECT DATEADD(DAY,(CASE DATEPART(DW,@PassedDate) WHEN 7 THEN 6 ELSE 6 - DATEPART(DW,@PassedDate) END),@PassedDate);

This is for SQL Server 2008. To use in 2005, just change the date fields to your preference for datetime to date conversions. It also assumes you are not changing the default week begin value.

DECLARE @PassedDate date = '5/21/2011';
SELECT DATEADD(DAY,(CASE DATEPART(DW,@PassedDate) WHEN 7 THEN 6 ELSE 6 - DATEPART(DW,@PassedDate) END),@PassedDate);
尛丟丟 2024-11-14 02:07:34

与最上面的答案类似,但在解决方案中没有使用 @@DATEFIRST

DECLARE @Today DATETIME = GETDATE(); -- any date
DECLARE @WeekdayIndex SMALLINT = DATEPART(WEEKDAY, @Today);
DECLARE @DaysUntilFriday SMALLINT = (13 - @WeekdayIndex) % 7;
DECLARE @UpcomingFridayDate DATETIME = DATEADD(DAY, @DaysUntilFriday, @Today);
SELECT @UpcomingFridayDate ;

Similar to the top answer, but without using @@DATEFIRST in the solution:

DECLARE @Today DATETIME = GETDATE(); -- any date
DECLARE @WeekdayIndex SMALLINT = DATEPART(WEEKDAY, @Today);
DECLARE @DaysUntilFriday SMALLINT = (13 - @WeekdayIndex) % 7;
DECLARE @UpcomingFridayDate DATETIME = DATEADD(DAY, @DaysUntilFriday, @Today);
SELECT @UpcomingFridayDate ;
眉黛浅 2024-11-14 02:07:34

这里有很好的解决方案,但我还建议查看时间表:您可以在分析服务器中轻松生成它们,并且它们可以非常快地建立索引,为您提供许多简单的方法来获取下周的日子(除其他外)。

您可以在此处找到有关它们的更多信息。

在我们的例子中,同样的解决方案当然

Select MIN(PK_Date) from Time Where PK_Date > @SomeDate AND Day_Of_Week= 6

,当您对大型记录集执行此操作时,您还可以进行连接以获得最大速度和速度。效率。

Great solutions here, but I also recommend looking at time tables: you can generate them easily in Analysis server, and they can be indexed to be very fast, giving you lots of easy ways to get next week days (among other things).

You can find out more about them here

In our case, the same solution would be

Select MIN(PK_Date) from Time Where PK_Date > @SomeDate AND Day_Of_Week= 6

And of course when you're doing this for a large recordset, you can also do joins for maximum speed & efficiency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文