Solved

Converting Excel formulas to SQL Case

Posted on 2014-03-06
14
819 Views
Last Modified: 2014-03-06
I would like to ask fro help converting a few excel formulas to sql syntax.

I have the task to use them in a stored procedure, but I am having difficulty converting them.

I have a sample of the excel file and the stored procedure I am using for testing, using hard coded values, same as the first row of the excel file.

There are 5 formulas in the following cells: D, J, K, L, M

Thank you very much for your help.
Sales-Order-Proration-Sample.xlsx
stored-procedure-sample.txt
0
Comment
Question by:metropia
  • 9
  • 4
14 Comments
 
LVL 12

Expert Comment

by:Harish Varghese
Comment Utility
Can you please state the requirements (formula) in words?
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Where are the formulas you want to convert?
0
 

Author Comment

by:metropia
Comment Utility
Formulas:

Number Of Days = IF(SalesOrderTypeCode="Blanket",IF(Today>ContractEndDate,1,IF(Today>ReportEndDate, 0,(IF(Today<ReportStartDate,(+C7ReportEndDate-ReportStartDate+1),(ReportEndDate-Today+1))))),(IF(OR(AND(RequestedDeliveryDate>=ReportStartDate,RequestedDeliveryDate<=ReportEndDate), AND(RequestedDeliveryDate>=Today,RequestedDeliveryDate<=ReportEndDate, Today>ReportStartDate)),1,0)))

Proration Quantity = +NumberOfDays * RemainingDailyQuantity

Original Contract Length 	= IF(SalesOrderTypeCode="Blanket",+ContractEndDate - ContractStartDate + 1, 0)

Remaining Contract Length = IF(SalesOrderTypeCode="Blanket",+ContractEndDate-Today+1,0)

Remaining Daily Quantity = IF(SalesOrderTypeCode="Standard",SalesOrderLineOpenQuantity,IF(Today>ContractEndDate,SalesOrderLineOpenQuantity,SalesOrderLineOpenQuantity/RemainingContractLength))

Open in new window

0
 

Author Comment

by:metropia
Comment Utility
The formulas are in the excel file I attached to this question.

The columns with formulas are: D, J, K, L, M
0
 

Author Comment

by:metropia
Comment Utility
I attached the excel file that contains the columns with the formulas, and a stored procedure I started writing, using the values of row 7 in the excel file.
0
 

Author Comment

by:metropia
Comment Utility
Thank you and sorry for the lack of clarity when typing my question
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 500 total points
Comment Utility
Here they are:
NumberOfDays = case when SalesOrderTypeCode='Blanket' then
	case when Today > ContractEndDate then 1
		when Today > ReportEndDate then 0
		When Today < ReportStartDate then ReportEndDate - ReportStartDate + 1,
		Else ReportEndDate - Today + 1
	End
Else
	case When (RequestedDeliveryDate >= ReportStartDate AND RequestedDeliveryDate <= ReportEndDate) 
				OR (RequestedDeliveryDate >= Today AND RequestedDeliveryDate <= ReportEndDate AND Today > ReportStartDate)
		Then 1,
	else 0 End
End

ProrationQuantity = NumberOfDays * RemainingDailyQuantity

OriginalContractLength = case when SalesOrderTypeCode = 'Blanket' then ContractEndDate - ContractStartDate + 1 Else 0 End

RemainingContractLength = case when SalesOrderTypeCode = 'Blanket' then ContractEndDate - Today + 1 else 0 end

RemainingDailyQuantity = 
	Case when SalesOrderTypeCode = 'Standard' Then SalesOrderLineOpenQuantity
		When Today > ContractEndDate Then SalesOrderLineOpenQuantity
		Else SalesOrderLineOpenQuantity/RemainingContractLength
	End
	

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:metropia
Comment Utility
Wow. Thank you so much.

I tried using the first converted formula for "NumberOfDays" and I get an error:

Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query.

Is it because the formula performs arithmetic with dates? would you be able to show me how to work around this problem?

Once again, thank you very much
0
 

Author Comment

by:metropia
Comment Utility
the error comes from this part:

when @Today < @ReportStartDate 
then @ReportEndDate - @ReportStartDate + 1
else @ReportEndDate - @Today + 1

Open in new window

0
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 500 total points
Comment Utility
Change the lines as below:
when @Today < @ReportStartDate 
then DATEDIFF(dd, @ReportStartDate, @ReportEndDate) + 1
else DATEDIFF(dd, @Today, @ReportEndDate) + 1

Open in new window

0
 

Author Comment

by:metropia
Comment Utility
Thank you.

There is something that works different, because in excel I get a different number:

DECLARE @ReportStartDate DATETIME
DECLARE @ReportEndDate DATETIME
SET @ReportStartDate = '03/15/2014'      -- Excel Cell B
SET @ReportEndDate = '03/31/2014'      -- Excel Cell C

SELECT DATEDIFF(dd, @ReportEndDate, @ReportStartDate) + 1  

Gives me -15

But excel calculation gives me 17 as the number of days
0
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 500 total points
Comment Utility
It should be
SELECT DATEDIFF (dd, @ReportStartDate, @ReportEndDate) + 1  
NOT SELECT DATEDIFF(dd, @ReportEndDate, @ReportStartDate) + 1
0
 

Author Comment

by:metropia
Comment Utility
Harish,

Would the funtion DATEDIFF also be used in the subtraction formula for the column "RemainingContractLength"?

RemainingContractLength = case when SalesOrderTypeCode = 'Blanket' then ContractEndDate - Today + 1 else 0 end
0
 

Author Closing Comment

by:metropia
Comment Utility
Impeccable work Harish. I really copied and pasted the code, made just a few changes and it worked smoothly.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

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

16 Experts available now in Live!

Get 1:1 Help Now