Solved

Datediff HALF YEAR?? and dateadd

Posted on 2008-10-16
6
1,194 Views
Last Modified: 2010-04-21
Guys it doesnt work right

declare @StartTime datetime
set @StartTime = '10/01/2008'

SELECT
CASE t2.HowToCharge
      WHEN 1 THEN CONVERT(char,DateAdd(m,DATEDIFF(m,t2.StartDate,@StartTime),t2.StartDate),101)
      WHEN 3 THEN CONVERT(char,DateAdd(q,DATEDIFF(q,t2.StartDate,@StartTime),t2.StartDate),101)
      WHEN 6 THEN CONVERT(char,DateAdd(q,2*DATEDIFF(q,t2.StartDate,@StartTime),t2.StartDate),101)
      WHEN 12 THEN CONVERT(char,DateAdd(year,DATEDIFF(year,t2.StartDate,@StartTime),t2.StartDate),101)
      END
NextInvoiceDate,

Am i missing something here. The half year doesnt work.

If i set @StartTime = 10/01/2008 And the date in the StartDate is 10/01/2008. NextInvoiceDate, will be 10/01/2008 as it should. But as this should be every half year, the next date should be 04/01/2009 RIGHT? but it isnt, if you change StartTime to 04/01/2009 NextInvoiceDate will be 10/01/2009. But if you change StartTime to 03/01/2009 NextInvoiceDate will be 04/01/2009. What am i doing wrong?


And another thing, how can i handle people going back in time. I mean if the StartTime is smaller than StartDate
0
Comment
Question by:tim_carter
  • 3
  • 2
6 Comments
 
LVL 1

Author Comment

by:tim_carter
ID: 22730993
Well i can se what im doing wrong.. You cant do it this way because 2*Datediff. will turn into years eventually. How can i get a datediff halfyear? anyone?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22738685
There's something missing from the problem definition here ...

You're not just trying to find half a year from the StartTime or half a year from the StartDate ... you're trying to find ... what?

If the start point is clear then adding either 2 quarters or 6 months should get you what you want.
0
 
LVL 1

Author Comment

by:tim_carter
ID: 22738916
Everymonth i will change the @startdate to the current date. (month). and the field NextInvoiceDate should tell me the date for each client when they should be invoiced. Now the Quarterly, and Yearly and ofcourse monthly works fine. But i cant seem to get the Helf Year working. Do you have an example of how to do that? Because the way i have done it in the example above, will never work.

Thanks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 22740360
The only time that 1/2 year (rounded since I assume you're not looking for down to the minute/second differences) isn't going to be 182 days is during a leap year in which it will be 183.  

So for the 1/2 year (dealing with leap year):

WHEN 6 THEN CONVERT(char,DateAdd(q,2*DATEDIFF(q,t2.StartDate,@StartTime),t2.StartDate),101)

convert(char(10), dateadd(d, (datediff(d,getdate(), dateadd(yy,1,getdate())))/2 ,t.2startdate),101)


But this all seems like overkill to get more precise than +6 months.
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 250 total points
ID: 22756965
Tim, can you lay out several start dates & start times along with what you get & what you want to get?  I think that's the piece were missing.

If I understand your original post correctly, it looks like this:

StartDate    @StartTime    CurrentAnswer    CorrectAnswer
10/01/2008  10/01/2008   10/01/2008            10/01/2008
10/01/2008  11/01/2008   10/01/2009             04/01/2009
0
 
LVL 1

Author Closing Comment

by:tim_carter
ID: 31506710
dont think it can be done, but thanks for the help guys
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now