Solved

Datediff HALF YEAR?? and dateadd

Posted on 2008-10-16
6
1,210 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query including math and 2 tables problem 26 56
SELECT query on two levels (detail and summary) 13 57
Currency in SQL? 2 30
RAISERROR WITH NOWAIT 2 16
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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