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
Solved

Converting Excel formulas to SQL Case

Posted on 2014-03-06
14
892 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
ID: 39909967
Can you please state the requirements (formula) in words?
0
 
LVL 33

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 500 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 500 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 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

809 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