SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to increment same calendar day next month
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Aged Yak Warrior

540 Posts

Posted - 04/26/2011 :  18:34:27  Show Profile  Reply with Quote
If i pass one month: with todays date(04/26/2010) then

the result has to be same date next month, is it possible?

same should happen if i say 3 months or a year.

same date after 3 monmths, same date after a year.


Set @FixedDays = 'Monthly' or 'Quarterly' or 'Yearly'
If @FixedDays = 'Monthly'
BEGIN
SET @RESULTDAYS = '05/26/2011'
END
ELSE If @FixedDays = 'Quarterly'
BEGIN
SET @RESULTDAYS = '07/26/2011'
END
ELSE If @FixedDays = 'Yearly'
BEGIN
SET @RESULTDAYS = '04/26/2012'
END

Thanks for the helpful info.

X002548
Not Just a Number

15586 Posts

Posted - 04/26/2011 :  18:37:18  Show Profile  Reply with Quote
OK...look at DATEADD function in BOL

However...are you sure you want the same day?

Months have diffent days...which gets worse by quarter and worse by year (adding in leaps)

Never mind that Tsunamis mess with our calendar as well



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 04/26/2011 :  18:40:27  Show Profile  Reply with Quote
Sure, just use dateadd

DECLARE @FixedDays varchar(20)
DECLARE @RESULTDAYS datetime
DECLARE @inDate datetime

SET @inDate = '20110426'

Set @FixedDays = 'Monthly' or 'Quarterly' or 'Yearly'
If @FixedDays = 'Monthly'
BEGIN
SET @RESULTDAYS = DATEADD(month,1,@inDate)
END
ELSE If @FixedDays = 'Quarterly'
BEGIN
SET @RESULTDAYS = DATEADD(q,1,@inDate)
END
ELSE If @FixedDays = 'Yearly'
BEGIN
SET @RESULTDAYS = DATEADD(y,1,@inDate)
END

select @inDate,@FixedDays,@resultdays


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 04/26/2011 :  18:42:30  Show Profile  Reply with Quote


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 04/26/2011 :  22:14:38  Show Profile  Reply with Quote
You will have to decide the rule for when the same day of month does not exist in the next month (see 2011-01-31 below).

select
	a.DT,
	NextMonth = dateadd(month,1,a.DT)
from
	( -- Test Data
	select DT = convert(date,'20110131')    union all
	select DT = convert(date,'20110215')    union all
	select DT = convert(date,'20121231')
	) a
order by
	a.DT


Results:
DT         NextMonth
---------- ----------
2011-01-31 2011-02-28
2011-02-15 2011-03-15
2012-12-31 2013-01-31

(3 row(s) affected)


CODO ERGO SUM

Edited by - Michael Valentine Jones on 04/26/2011 22:18:38
Go to Top of Page

cplusplus
Aged Yak Warrior

540 Posts

Posted - 04/27/2011 :  12:07:50  Show Profile  Reply with Quote
Hello Jim,
Thanks a lot for the script, but has one problem.
Monthly & quarterly both working except Yearly.

When i choose yearly the year remaining same 2011, instead of showing 2012.

Thanks.
Go to Top of Page

robvolk
Most Valuable Yak

USA
15676 Posts

Posted - 04/27/2011 :  12:16:32  Show Profile  Visit robvolk's Homepage  Reply with Quote
SET @RESULTDAYS = DATEADD(year,1,@inDate)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000