问题描述
我需要的是今天的下一个特定日期(星期一,星期二,星期三...)的日期。
允许用户选择一天后,他们想要,并将它作为一个int存储在表中。 下周二打电话给我(3)
星期日= 1
星期一= 2
星期二= 3
...
所以我的表看起来像这样。
UserID,NextDayID
什么我想出的是:
select dateadd(dd,(7 - datepart(dw,GETDATE())+ NextDayID) %7,getdate())
似乎有效,如果您要求接下来无论今天如何,如果需要,我可以添加一个星期。
我想知道的是,这是一个很好的解决方案,还是有一些我失踪的东西?
1)您的解决方案使用非确定性函数: datepart(dw ...) / code>。由于这个方面,更改
DATEFIRST
设置将会产生不同的结果。例如,您应该尝试:
SET DATEFIRST 7;
你的解决方案;
然后
code> SET DATEFIRST 1;
你的解决方案;
2)以下解决方案独立于 DATEFIRST
/ LANGUAGE
设置:
DECLARE @NextDayID INT = 0 - 0 = Mon,1 = Tue,2 = Wed,...,5 = Sat,6 = Sun
SELECT DATEADD(DAY,(DATEDIFF(DAY,@NextDayID,GETDATE())/ 7)* 7 + 7 ,@NextDayID)AS NextDay
结果:
NextDay
-----------------------
2013-09-23 00: 00:00.000
此解决方案基于以下属性 DATETIME
类型:
-
第0天=
19000101
= Mon -
第1天=
19000102
= Tue -
第2天=
19000103
= Wed
...
-
第5天=
19000106
= Sat -
第6天=
19000107
= Sun
因此,将INT值0转换为DATETIME会给出 19000101
。
如果你想找到下一个星期三
,那么你应该从第2天开始( 19000103
/ Wed
),计算第2天和当天之间的天数( 20130921
; 41534天),除以7(为了获得全周数; 5933周),乘以7(41531票;为了获得天数 - 第一个周三之前的整周) code> /
19000103
和最后一个星期三
),然后添加7天(一周; 41538天;为了获得以下星期三
)。将此号码(41538天)添加到开始日期: 19000103
。
注意:我当前的日期是 20130921
。
编辑#1:
DECLARE @NextDayID INT;
SET @NextDayID = 1; - 下周日
SELECT DATEADD(DAY,(DATEDIFF(DAY,((@NextDayID + 5)%7),GETDATE())/ 7)* 7 + 7,((@NextDayID + 5)%7 ))AS NextDay
结果:
NextDay
-----------------------
2013-09-29 00:00: 00.000
注意:我目前的日期是 20130923
。
What I need is a date for the next given day (Monday, Tuesday, Wed...) following today's date.
The user is allowed to select what day following they want and that is stored as an int in a table. "Call me next Tuesday (3)"
Sunday = 1
Monday = 2
Tuesday = 3
...
So my table looks like this.
UserID, NextDayID
What I have come up with is:
select dateadd(dd,(7 - datepart(dw,GETDATE()) + NextDayID ) % 7, getdate())
It seems to work and will return today's date if you ask for the next whatever day today is which I can add a week if needed.
What I am wondering is, is that a good solution or is there something that I'm missing?
1) Your solution uses a non-deterministic function: datepart(dw...)
. Because of this aspect, changing DATEFIRST
setting will gives different results. For example, you should try:
SET DATEFIRST 7;
your solution;
and then
SET DATEFIRST 1;
your solution;
2) Following solution is independent of DATEFIRST
/LANGUAGE
settings:
DECLARE @NextDayID INT = 0 -- 0=Mon, 1=Tue, 2 = Wed, ..., 5=Sat, 6=Sun
SELECT DATEADD(DAY, (DATEDIFF(DAY, @NextDayID, GETDATE()) / 7) * 7 + 7, @NextDayID) AS NextDay
Result:
NextDay
-----------------------
2013-09-23 00:00:00.000
This solution is based on following property of DATETIME
type:
Day 0 =
19000101
= MonDay 1 =
19000102
= TueDay 2 =
19000103
= Wed
...
Day 5 =
19000106
= SatDay 6 =
19000107
= Sun
So, converting INT value 0 to DATETIME gives 19000101
.
If you want to find the next Wednesday
then you should start from day 2 (19000103
/Wed
), compute days between day 2 and current day (20130921
; 41534 days), divide by 7 (in order to get number of full weeks; 5933 weeks), multiple by 7 (41531 fays; in order to get the number of days - full weeks between the first Wednesday
/19000103
and the last Wednesday
) and then add 7 days (one week; 41538 days; in order to get following Wednesday
). Add this number (41538 days) to the starting date: 19000103
.
Note: my current date is 20130921
.
Edit #1:
DECLARE @NextDayID INT;
SET @NextDayID = 1; -- Next Sunday
SELECT DATEADD(DAY, (DATEDIFF(DAY, ((@NextDayID + 5) % 7), GETDATE()) / 7) * 7 + 7, ((@NextDayID + 5) % 7)) AS NextDay
Result:
NextDay
-----------------------
2013-09-29 00:00:00.000
Note: my current date is 20130923
.
这篇关于SQL Server:下一个相对的星期几。 (下周一,周二,周三.....)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!