SiSense BI Evolved
Got a SQL Server or .NET question? Discuss it in the forums. (SQL Server Forums | Dot NET Forums)
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

Find alternate week day

My co-worker had a task of finding minimum alternate week day as of today for a given date and day. Consider these data

  • Date : 2009-12-22
  • day : 5 (it denotes Thursday)

It means the event was scheduled on 2009-12-22 which should run on every alternate Thursday starting from 2009-12-24 (The first Thursday on or after the scheduled date). The task is finding the minimum of current available day Here is my solution

declare @date datetime, @day int 
select @date='2009-12-22',@day=5

select 
        min(dateadd(day,14*(number),date)) 
from 
        (
                select dateadd(day,@day-datepart(weekday,@date),@date) as date
        ) as t,master..spt_values
where type='p' and dateadd(day,14*(number),date)>=getdate()

If you like this article,  Subscribe in a reader or Subscribe by Email. Show your support by sharing this article with your friends through the services given below.

Share

Published 01-12-2010 9:45 PM by Madhivanan
Filed under: , ,

Comments

# Beyond Relational Newsletter - 1 Feb 2010 - TSQL Challenge 22, BI Weekly Capsule, BI resource center and SQL Azure FAQ

Monday, February 01, 2010 1:20 PM by Beyond Relational - News Letters

February 1, 2010 [Volume 2, Issue 3] TSQL Challenge 22 - Filling balls into boxes based on rules and

Copyright © Beyondrelational.com