SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Date to month
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  10:17:41  Show Profile  Reply with Quote
I have a 2 date fields 1 is start_date and the other is expire_date. I need to have a expire_date - start_date give me total months. Then I need to calculate the Annual_Value. Anything under 12 months should just show Price as Annual_value. Anything over 12 months should be (Price/Months)*12. Annual_Value and Months are new fields being created from the calculations.

Example is
Start_date Expire_date Months Price Annual_Value
1/31/2013 3/26/2013 2 450.00 450.00
1/31/2013 3/24/2016 34 6000.00 2117.65

djj55
Constraint Violating Yak Guru

USA
341 Posts

Posted - 09/12/2013 :  10:39:03  Show Profile  Reply with Quote
Look at DATEDIFF(MONTH, Start_date, Expire_date)

djj
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  13:00:13  Show Profile  Reply with Quote
works and also doesn't work I notice something I should have taken into consideration. example.


start date exp date Months
5/1/2013 4/30/2016 35(this actually should be 36 because its the whole month of April) How would I fix this.


SELECT

DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) AS Contract_Months
,dbo.sa.price
,CASE WHEN DATEDIFF(MONTH, [rnu-date], [exp-date]) > '12' THEN ((price / DATEDIFF(MONTH,[rnu-date], [exp-date])) * 12) ELSE price END AS Annual_Value


FROM dbo.sa LEFT OUTER JOIN
dbo.customer ON dbo.sa.[cust-no] = dbo.customer.[Cust-no]
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 09/12/2013 :  13:21:41  Show Profile  Visit SwePeso's Homepage  Reply with Quote
1 + DATEDIFF(, , ) ???

Because DATEDIFF(DAY, ThisDay, ThisDay) equals 0.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  13:29:03  Show Profile  Reply with Quote
Ok so I was thinking about this a little more and decided that this might work with a Case when I just need help writing it.

Case when DATEDIFF(DAY, [rnu-date], [exp-date])< '15' THEN DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) ELSE (DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) + 1) End AS Contract_Months
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  13:37:58  Show Profile  Reply with Quote
Ok my code worked but now I need to figure out how to fix the next piece by nesting case in the next line of code.

Case when DATEDIFF(DAY, [rnu-date], [exp-date])< '15' THEN DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) ELSE (DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) + 1) End AS Contract_Months

This Line needs to reflect the same example as the line above it


CASE WHEN DATEDIFF(MONTH, [rnu-date], [exp-date]) > '12' THEN ((price / DATEDIFF(MONTH,[rnu-date], [exp-date])) * 12) ELSE price END AS Annual_Value


Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
341 Posts

Posted - 09/12/2013 :  13:54:27  Show Profile  Reply with Quote
You may want to use a cte where you calculate the number of months (once) and can you that value in your case statements.

djj
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  14:04:52  Show Profile  Reply with Quote
How would I write that.

SELECT dbo.sa.[cust-no], dbo.customer.name, dbo.sa.[sa-no], dbo.sa.[sslspn-code], dbo.salesper.[Slspn-name], CONVERT(VARCHAR(10), dbo.sa.[orig-date], 101)
AS Create_date, CONVERT(VARCHAR(10), dbo.sa.[rnu-date], 101) AS Start_date, CONVERT(VARCHAR(10), dbo.sa.[exp-date], 101) AS [exp-date], dbo.sa.Status,
dbo.sa.[div-code], CASE WHEN DATEDIFF(DAY, [rnu-date], [exp-date]) < '15' THEN DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) ELSE (DATEDIFF(MONTH,
dbo.sa.[rnu-date], dbo.sa.[exp-date]) + 1) END AS Contract_Months, dbo.sa.price, CASE WHEN DATEDIFF(MONTH, [rnu-date], [exp-date])
> '12' THEN ((price / DATEDIFF(MONTH, [rnu-date], [exp-date])) * 12) ELSE price END AS Annual_Value
FROM dbo.sa LEFT OUTER JOIN
dbo.salesper ON dbo.sa.[sslspn-code] = dbo.salesper.[Slspn-code] LEFT OUTER JOIN
dbo.customer ON dbo.sa.[cust-no] = dbo.customer.[Cust-no]
WHERE (dbo.sa.[div-code] = '23')
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  14:11:46  Show Profile  Reply with Quote
WITH sa_CTE
AS
(SELECT dbo.sa.[cust-no], dbo.customer.name, dbo.sa.[sa-no], dbo.sa.[sslspn-code], dbo.salesper.[Slspn-name], CONVERT(VARCHAR(10), dbo.sa.[orig-date], 101)
AS Create_date, CONVERT(VARCHAR(10), dbo.sa.[rnu-date], 101) AS Start_date, CONVERT(VARCHAR(10), dbo.sa.[exp-date], 101) AS [exp-date], dbo.sa.Status,
dbo.sa.[div-code], CASE WHEN DATEDIFF(DAY, [rnu-date], [exp-date]) < '15' THEN DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) ELSE (DATEDIFF(MONTH,
dbo.sa.[rnu-date], dbo.sa.[exp-date]) + 1) END AS Contract_Months, dbo.sa.price
FROM dbo.sa LEFT OUTER JOIN
dbo.salesper ON dbo.sa.[sslspn-code] = dbo.salesper.[Slspn-code] LEFT OUTER JOIN
dbo.customer ON dbo.sa.[cust-no] = dbo.customer.[Cust-no]
WHERE (dbo.sa.[div-code] = '23'))

SELECT [cust-no], name, sa-no],[sslspn-code], Slspn-name], Create_date, Start_date, [exp-date], Status, [div-code], Contract_Months, price, CASE WHEN Contract_Months
> '12' THEN ((price / Contract_Months) * 12) ELSE price END AS Annual_Value
From sa_CTE
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  14:11:56  Show Profile  Reply with Quote
like that?
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
341 Posts

Posted - 09/12/2013 :  14:16:01  Show Profile  Reply with Quote
That should work.

djj
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  14:18:23  Show Profile  Reply with Quote
nope lol
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

Posted - 09/12/2013 :  14:22:47  Show Profile  Reply with Quote
Stupid mistake. figured it out. I forgot to add the Fields after the First With sa_CTE


WITH sa_CTE ([cust-no], name, [sa-no],[sslspn-code], [Slspn-name], Create_date, Start_date, [exp-date], Status, [div-code], Contract_Months, price)
AS
(SELECT dbo.sa.[cust-no], dbo.customer.name, dbo.sa.[sa-no], dbo.sa.[sslspn-code], dbo.salesper.[Slspn-name], CONVERT(VARCHAR(10), dbo.sa.[orig-date], 101)
AS Create_date, CONVERT(VARCHAR(10), dbo.sa.[rnu-date], 101) AS Start_date, CONVERT(VARCHAR(10), dbo.sa.[exp-date], 101) AS [exp-date], dbo.sa.Status,
dbo.sa.[div-code], CASE WHEN DATEDIFF(DAY, [rnu-date], [exp-date]) < '15' THEN DATEDIFF(MONTH, dbo.sa.[rnu-date], dbo.sa.[exp-date]) ELSE (DATEDIFF(MONTH,
dbo.sa.[rnu-date], dbo.sa.[exp-date]) + 1) END AS Contract_Months, dbo.sa.price
FROM dbo.sa LEFT OUTER JOIN
dbo.salesper ON dbo.sa.[sslspn-code] = dbo.salesper.[Slspn-code] LEFT OUTER JOIN
dbo.customer ON dbo.sa.[cust-no] = dbo.customer.[Cust-no]
WHERE (dbo.sa.[div-code] = '23'))

SELECT [cust-no], name, [sa-no],[sslspn-code], [Slspn-name], Create_date, Start_date, [exp-date], Status, [div-code], Contract_Months, price, CASE WHEN Contract_Months
> '12' THEN ((price / Contract_Months) * 12) ELSE price END AS Annual_Value
From sa_CTE
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000