[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Converting Excel formulas to SQL Case

Posted on 2014-03-06
14
Medium Priority
?
1,091 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
[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
  • 9
  • 4
14 Comments
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39909967
Can you please state the requirements (formula) in words?
0
 
LVL 35

Expert Comment

by:Norie
ID: 39909977
Where are the formulas you want to convert?
0
 

Author Comment

by:metropia
ID: 39910031
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:metropia
ID: 39910034
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
ID: 39910039
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
ID: 39910051
Thank you and sorry for the lack of clarity when typing my question
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 2000 total points
ID: 39910153
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
 

Author Comment

by:metropia
ID: 39910173
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
ID: 39910188
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 2000 total points
ID: 39910214
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
ID: 39910258
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 2000 total points
ID: 39910275
It should be
SELECT DATEDIFF (dd, @ReportStartDate, @ReportEndDate) + 1  
NOT SELECT DATEDIFF(dd, @ReportEndDate, @ReportStartDate) + 1
0
 

Author Comment

by:metropia
ID: 39910502
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
ID: 39910535
Impeccable work Harish. I really copied and pasted the code, made just a few changes and it worked smoothly.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

650 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