Solved

Datediff HALF YEAR?? and dateadd

Posted on 2008-10-16
6
1,186 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
dont think it can be done, but thanks for the help guys
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

744 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

11 Experts available now in Live!

Get 1:1 Help Now