Solved

Datediff HALF YEAR?? and dateadd

Posted on 2008-10-16
4
1,238 Views
Last Modified: 2008-10-16
Hi Guys

i have this query

DateAdd(q,DATEDIFF(q,t2.StartDate,getdate()),t2.StartDate) NextInvoiceQuarter,

this gives me the nextquarter invoice date.
Now my problem is that sum customers needs to be charged every half year. How do i calculate that? it doesnt seem like datediff has a half year function?
0
Comment
Question by:tim_carter
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22729660
half year = 6 months or 2 quarters ...
so, where is the problem?
0
 
LVL 6

Accepted Solution

by:
openshac earned 500 total points
ID: 22729702
Try the following:

Of course you may need to pull the value 2 or 6 from another column in your query.

You could have a column called CustomerChargedEveryNQuarters which has values 1 or 2

DateAdd(q, 2 * DATEDIFF(q, t2.StartDate,getdate()),t2.StartDate) NextInvoiceQuarter
 
or
 
DateAdd(m, 6 * DATEDIFF(m, t2.StartDate,getdate()), t2.StartDate) NextInvoiceQuarter
 
or even
 
DateAdd(q, CustomerChargedEveryNQuarters  * DATEDIFF(q, t2.StartDate,getdate()), t2.StartDate) NextInvoiceQuarter

Open in new window

0
 
LVL 1

Author Comment

by:tim_carter
ID: 22729823
Thanks guys

DateAdd(quarter,DATEDIFF(quarter,t2.StartDate,'10/14/2008'),t2.StartDate) NextInvoiceQuarter,
DateAdd(quarter,2*DATEDIFF(quarter,t2.StartDate,'10/14/2008'),t2.StartDate) NextInvoiceHalfYear,
DateAdd(year,DATEDIFF(year,t2.StartDate,'10/14/2008'),t2.StartDate) NextInvoiceYear,

works perfectly
0
 
LVL 1

Author Comment

by:tim_carter
ID: 22730522
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?

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

Suggested Solutions

Title # Comments Views Activity
SQL Server syntax question 13 32
Sql server function help 15 31
Query Help - MSSQL - Averages 5 27
Separate 2 comma delimited columns into separate rows 2 32
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.​
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
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…

777 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