What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12520 |  Welcome, Guest!   Register  Login
 Home > Coding Horrors > SQL Server > DATEADD FUnction ...
Manicse

DATEADD FUnction

 Coding Horror posted by: Manicse | Posted on: 7/9/2012 | Category: SQL Server | Views: 2078 | Level: Starter | Status: [Member] | Points: 75 | Alert Moderator   



Recently i have got a error in my application.
My application is based on banking. While payment happened in every month we need to move the next payment date to the next month.

Consider a scenario,

Payment Date is : 20th Aug

So if he pays for August then the Next payment date will be 20th Sep using the function in sql.
  • DATEADD(M,1,date)

Now if a user is paying for the month of January, his current next date is

Payment Date: 30th Jan

Now we are adding the DateADD function.

DATEADD(M,1,'01/30/2012')  -----> Output will be 29th FEB(for Leap Year) and 28th FEB(Non Leap Years)

So all the dates will be wrong with the further Dateadd() function.






Mani.R
Found interesting? Add this to:


About Manigandan R

Experience:3 year(s)
Home page:http://www.dotnetfunda.com
Member since:Friday, July 06, 2012
Level:Starter
Status: [Member]
Biography:
 Responses
Gopesh9
Posted by: Gopesh9 | Posted on: 8/3/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

You can check whether the entered year is leap year or not, when you are adding the month to January.
But i think that its the property of SQL Server, it will automatically check that the year entered is leap year or not.

G. S.
.Net Developer

Suhesh
Posted by: Suhesh | Posted on: 10/7/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

Try to keep the initial payment date and increase the month interval. like DateAdd(M,interval(go like 1,2,3...every month),Initial Due Date).

Sandeepmhatre
Posted by: Sandeepmhatre | Posted on: 12/18/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

my simple suggestion to this question is instead of adding months to the next date you simply add 30 / 31 / 29 / 28 according to month.

-- Code To find the Financial Year
Declare @FinYear nVarchar(20)
Declare @Date Datetime

--Set @Date = '2012-01-29 00:00:00.000'
Set @Date = '2012-03-29'

select DATEADD(d,30,@Date )
this will work.

Sandeep M,
Software Developer
Follow me on :
http://sandeepmhatre.blogspot.in

>> Write Response - Respond to this post and get points

More Coding Horrors

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 5:12:15 PM