调整日期以匹配保存的星期几
我有一个表,用于存储开始日期和开始日期所在的星期几的名称。我不知道为什么,这是糟糕的设计,但我没有创建它,也无法更改它。当然,现在我们有一些日期与星期几不匹配。更糟糕的是,星期几是正确的,而开始日期是不正确的。所以我需要做的是调整日期,使每行的 StartDate 落在该行的 DayOfWeek 上。我们可以假设 StartDate 始终是最小值,因此目标日期将是当前设置的 StartDate 之后的第一个 [DayOfWeek]。
例如,我的行看起来像这样(8/23/10 是星期一,8/29/10 是星期日):
StartDate DayOfWeek
-----------------------
2010-08-23 Monday
2010-08-23 Tuesday
2010-08-29 Thursday
在第 2 行中,您可以看到日期应该是星期二,但实际上是星期一。我需要这样结束:
StartDate DayOfWeek
-----------------------
2010-08-23 Monday
2010-08-24 Tuesday
2010-09-02 Thursday
我在处理日期时总是很困难,但 SQL 也不是我最强的技能。谢谢。
I have a table that stores a StartDate and the name of the day of week that start date falls on. I don't know why, it is bad design but I didn't create it and can't change it. So of course, now we have some dates that don't match the day of week. To make it worse, the day of week is correct and the start date is incorrect. So what I need to do is adjust the dates so that each row's StartDate falls on that row's DayOfWeek. We can assume StartDate is always the minimum value so the target date will be the first [DayOfWeek] after the currently set StartDate.
So for example I have rows that look like this (8/23/10 was a Mon, 8/29/10 was a Sun):
StartDate DayOfWeek
-----------------------
2010-08-23 Monday
2010-08-23 Tuesday
2010-08-29 Thursday
In row 2 you can see the date is supposed to be a Tuesday but it's actually a Monday. I need to end up with this:
StartDate DayOfWeek
-----------------------
2010-08-23 Monday
2010-08-24 Tuesday
2010-09-02 Thursday
I always struggle when working with dates, but SQL is also not my strongest skill either. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
窃取geofftnz的设置,并希望这是他正在考虑的“聪明”方法:
对于第三行,我得到的日期是九月,而不是八月,但我认为结果是正确的,而你的样本结果是不正确的?
Stealing geofftnz's setup, and hoping this is the "clever" method he was thinking of:
For the third row, I'm getting a date in September, not August, but I think that the result is right and your sample result is incorrect?
将会有一种聪明的方法来做到这一点,并且有一种“让我们把一些数据敲出来”的方法。这是后者:
There will be a clever way of doing this and a "let's just bash some data out" way. Here is the latter: