user749448
user749448

Reputation: 189

How to get NextDayofWeek if you pass the date?

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

Upvotes: 0

Views: 169

Answers (3)

Ben Thul
Ben Thul

Reputation: 32697

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).

Upvotes: 2

M.R.
M.R.

Reputation: 4827

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

Upvotes: 1

ShaneBlake
ShaneBlake

Reputation: 11096

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...

Upvotes: 0

Related Questions