Solved

Datediff HALF YEAR?? and dateadd

Posted on 2008-10-16
6
1,219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
T-sql Date Format 9 67
SQL query to select row with MAX date 7 68
Syntax error creating JSON recordset 4 43
Where clause to fliter varchar with Characters 12 59
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…
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.​
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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