How to get NextDayofWeek if you pass the date?

淺唱寂寞╮ 提交于 2019-12-25 06:35:14

问题


Here's what I am trying to do. I will change the following code into SP which takes two parameter @startdate, and @transactionDate, and it will return the NextTransactiondate. The logic is @startdate determine which day of the week it is. The @NexttransactionDate should be equal to the day following the transactiondate. so in this example, the startday is Wednesday so the next transaction date should be - 2011-05-04'. In the code below, it is always computing to friday, but it should be dynamically compute based on the day. Any help is appreciated?

declare @TransactionDate datetime
declare @startDate datetime
declare @startDay int
declare @NextTransactionDate datetime
--Monday
set @TransactionDate = '2011-05-02'
--Wednesday
set @startDate = '2011-04-27'
set @startDay = datepart(dw,@startDate)

set @NextTransactionDate= DATEADD(DAY,(CASE DATEPART(DW,@TransactionDate)
   WHEN 7 THEN 6  
   WHEN 6 THEN 7  
    ELSE 6 - DATEPART(DW,@TransactionDate)
  END),@TransactionDate);  

print @NextTransactionDate

回答1:


The following works for me:

declare @TransactionDate DATETIME
DECLARE @TransactionDay tinyint
declare @startDate datetime
declare @startDay int
declare @NextTransactionDate datetime
--Monday
set @TransactionDate = '2011-05-05'
SET @TransactionDay = DATEPART(dw, @TransactionDate)
--Wednesday
set @startDate = '2011-04-27'
set @startDay = datepart(dw,@startDate)



set @NextTransactionDate= DATEADD(DAY, ((@startDay - @TransactionDay) + 7) % 7 ,@TransactionDate);  

select @startDay, DATEPART(dw, @NextTransactionDate), @NextTransactionDate

To explain the meat of it, I'm finding the difference in the day-of-week for the startDate and the transactionDate. I add 14 to it because negative numbers modulo positive numbers result in a negative number, which would put your next transaction date in the past (and you don't want that). The worst case is when @startDay is 1 and @TransactionDay is 7 which leads to a difference of -6. Adding 7 ensures that that difference is positive but still in the same equivalence class as the actual difference in the ring n mod 7(sorry... I'm a bit of a math nerd).




回答2:


Try this:


declare @TransactionDate datetime 
declare @startDate datetime 
declare @startDay int 
declare @transactionDay int 
declare @NextTransactionDate datetime 
declare @daysToAdd int

--Monday 
set @TransactionDate = '2011-05-02' 
set @transactionDay = datepart(dw,@TransactionDate)  

--Wednesday 
set @startDate = '2011-04-27' 
set @startDay = datepart(dw,@startDate)  

print @transactionDay
print @startDay

if(@startDay <= @transactionDay)
    set @daysToAdd = (@startDay + 7) - @transactionDay
else
   set @daysToAdd = @startDay - @transactionDay

set @NextTransactionDate = Dateadd(Day,@daysToAdd,@TransactionDate)
print @NextTransactionDate




回答3:


I'm not sure I follow what you're saying. I think you're saying that the next @TransactionDate, should be the next occurence of the day of the week that the @startDate falls on.

If that's the case, you could try :

declare @tDay = datepart(w, @transactionDate)

set @NextTransactionDate = DATEADD(w, @startDay-@tDay, @TransactionDate);

I'm not sure what you're doing with the 6 and 7 though... are you trying to make sure the new @TransactionDate is not a weekend? If so, this would need to be slightly modified...



来源:https://stackoverflow.com/questions/6077352/how-to-get-nextdayofweek-if-you-pass-the-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!