Solved

Determine next b-weekly date

Posted on 2016-10-26
12
89 Views
Last Modified: 2016-11-09
I need to determine what the next day of the month is that falls exactly on a two week interval from a given date  

 Example...
 A person has a payment to make
 First Payment was on July 20th 2010
 Interval is Bi-Weekly (Every 2 weeks)
When is the next date that two week interval would occur on?
0
Comment
Question by:lrbrister
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41861078
That would be DATEADD

SELECT DATEADD(d, 14, FirstPaymentDate) as NextPaymentDate
FROM YourTable

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41861083
Is your ultimate requirement to create rows for an entire time series with rows 14 days apart, or just get the next one?
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41861087
Hi lrbrister,

SELECT GETDATE() 'Today', DATEADD(week,2,GETDATE()) 'Today + 2 Weeks';

SELECT GETDATE() 'Today', DATEADD(week,2,GETDATE())  +1 'Today + 2 Weeks + oneDay';

Regards,
Tapan Pattanaik
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 41

Expert Comment

by:pcelba
ID: 41861117
I would extend previous answers by the first payment exact date specification:

DECLARE @payCheck Date
SET @payCheck = '2016-07-20'  

SELECT @payCheck AS PayCheck, DATEADD(week, 2, @payCheck) AS NextDate
0
 

Author Comment

by:lrbrister
ID: 41861178
Hey guys...
The "PaymentDate" is actually the FIRST PaymentDate

So...
On Bi-Weekly... that started on a Tuesday on July 2 2010
Does TODAY  fall on a every two weeks date

Because it's every 14 days... it will of course very seldom be on the 2nd, the 16th and the 30th every month.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41861182
In that case you're going to need some kind of a loop that begins at the start date and ends at whatever the term date is.   So for how long is the term?  One year, 30 years, ... ?

Check out SQL Server Calendar Table and do a control-Find on 'WHILE @i <= @total_days'.  You'll need to do something like this, either inserting rows into a temporary table and then selecting that table at the end of your code, or just inserting.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 41861217
When On Bi-Weekly... that started on a Tuesday on July 20 2010
Does TODAY  fall on a every two weeks date?

No. Because today is Wednesday.
0
 

Author Comment

by:lrbrister
ID: 41861220
pcelba Yeah... but if today was the same day of the week as July 20 2010 BUT it was not an even multiple of 14 days difference  it would still be a no
0
 
LVL 41

Accepted Solution

by:
pcelba earned 250 total points
ID: 41861296
This you may calculate very simply as a remainder when you divide the number of days by 14:

DECLARE @payCheck Date
SET @payCheck = '2010-07-20'  

SELECT DATEDIFF(day, @payCheck, GETDATE()) AS NoOfDays,
              CASE DATEDIFF(day, @payCheck, GETDATE()) % 14 WHEN 0 THEN 'YES' ELSE 'no' END AS  'PayDateToday'
0
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 250 total points
ID: 41861328
Check this: This code checks today's date and calculates the number of weeks passed.
Next date is determined by checking the number of weeks, odd or even.
declare @dt datetime	--first date
declare @today as datetime -- todays' date
declare @w int -- weeks from start date to today
declare @n datetime -- next date in bi weekly interval
select @dt='2016-07-20'; -- example
select @today=sysdatetime();

select sysdatetime(), @dt;

SELECT dateadd(WEEK,2,@dt);
select @w=DATEDIFF(WEEK,@dt,@today)
if @w=(@w/2)*2
	SELECT @n=dateadd(WEEK,@w,@dt)
ELSE
	SELECT @n=dateadd(WEEK,(@w/2)*2,@dt)

	SELECT @dt, @n, @w

Open in new window

0
 

Author Comment

by:lrbrister
ID: 41870990
Hey guys...
I kind of blended everything you guys had and came up with this function.
It seems to work fine... you folks see anything glaringly wrong?


USE [EverywareV3]
GO

/****** Object:  UserDefinedFunction [dbo].[IsPaymentDate]    Script Date: 11/2/2016 7:21:30 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- ======================================================================================
-- Author:		lbrister-- Create date: 10.26.2016
-- Description:	Determine if today is payment date
-- SELECT dbo.IsPaymentDate('Weekly','09/28/2016')
-- SELECT dbo.IsPaymentDate('Monthly','09/28/2016')
-- SELECT dbo.IsPaymentDate('Bi-Weekly','10/12/2016')
-- SELECT dbo.IsPaymentDate('Bi-Monthly','09/28/2016') --Returns yes on 1 and 15th
-- ======================================================================================
ALTER FUNCTION [dbo].[IsPaymentDate]
	   (
		@Interval VARCHAR(12) ,
		@PaymentDate DATETIME
	   )
RETURNS VARCHAR(10)
AS
	BEGIN
	-- Declare the return variable here
		DECLARE	@ResultVar VARCHAR(10) = '';

		DECLARE	@Today DATETIME = CONVERT(VARCHAR(10), GETDATE(), 101);
		DECLARE	@PaymentDayOfWeek INT = DATEPART(dw, @PaymentDate);
		DECLARE	@CurrentDayOfWeek INT = DATEPART(dw, GETDATE());
		DECLARE	@BiWeeklyDate BIT = DATEDIFF(d, @PaymentDate, CAST(GETDATE() AS DATE)) % 14;

		SET @ResultVar = (
						  SELECT	CASE WHEN @Interval IN ('Weekly')
											  AND @CurrentDayOfWeek = @PaymentDayOfWeek THEN 'YES'
										 WHEN @Interval IN ('Monthly')
											  AND DAY(@PaymentDate) = DAY(GETDATE()) THEN 'YES'
										 WHEN @Interval = 'Bi-Weekly'
											  AND @BiWeeklyDate = 0 THEN 'YES'
										 WHEN @Interval = 'Bi-Monthly'
											  AND DAY(GETDATE()) IN (1, 15) THEN 'YES'
										 ELSE 'NO'
									END
						 );

	-- Return the result of the function
		RETURN @ResultVar;

	END;

GO

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 41880603
Sorry for the late get back.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 61
2016 SQL Licensing 7 41
How to debug a store procedure in MS SQL 2008? 3 10
SQL Function NOT ROUND 9 10
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question