Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to execute an string expression like (7*8/4+1) inside a function how can i achieve it inside a function.
Set @variable = (@Expr) is not helping, as @variable is considering @Expr as a string variable and not a command.
Please Note : I have append the expression as 'Select ' +@Expr.
What wrong am i doing here. Most of the things i found in the internet are of no use as i cant use EXEC inside a function as it returns only if the command is executed or not rather than the result of the command.
How can i make the @expr to behave as an sql command when it comes to the line
SET @variable = (@expr).
Please guide me through

What I have tried:

declare @evaluated decimal(18,2)
declare @vexpr varchar(MAX) = ((15 * 4) + 1)
declare @formula varchar(max) = @vexpr
SET @formula = 'Select ' +@formula
SET @formula = REPLACE(@formula , '''', '')
SET @evaluated = (@formula)

return @evaluated;

Code posted any leads or helps

When i execute the above function i get an error as below :-
Error converting data type varchar to numeric.
Posted
Updated 23-Apr-18 18:57pm
v4
Comments
Patrice T 23-Apr-18 9:08am    
Show your code.
Yogi@FLG 24-Apr-18 0:22am    
declare @evaluated decimal(18,2)
declare @vexpr varchar(MAX) = ((15 * 4) + 1)
declare @formula varchar(max) = @vexpr
SET @formula = 'Select ' +@formula
SET @formula = REPLACE(@formula , '''', '')
SET @evaluated = (@formula)

return @evaluated;

Code posted any leads or helps
Patrice T 24-Apr-18 2:40am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Yogi@FLG 24-Apr-18 0:33am    
When i execute the above function i get an error
Error converting data type varchar to numeric.
Yogi@FLG 24-Apr-18 0:33am    
When i execute the code i get the error

Error converting data type varchar to numeric.

1 solution

SQL
CREATE TABLE #TEMP(formula NVARCHAR(500))
INSERT INTO #TEMP(formula ) 
    VALUES('((15 * 4) + 1)'),('((15/4) + 1)'),('((15 * 4) *(1+7))');

DECLARE @Grandtot NVarchar(500),@Query NVARCHAR(MAX)

SELECT @Grandtot=STUFF((SELECT '+CAST('+formula+' AS FLOAT)' FROM #TEMP FOR XML 
                     PATH(''),type).value('.','NVARCHAR(500)'),1,1,'');

SET @Query=N'SELECT '+@Grandtot+' AS GrandTot';

EXEC(@Query);



OUTPUT:
------
GrandTot
-----
545
 
Share this answer
 
v2
Comments
Yogi@FLG 24-Apr-18 2:03am    
But i need to call this from a function and functions do not support SPs.
Any other work around for the problem?
CHill60 24-Apr-18 4:59am    
If you are trying to call this from a function, should that calling function not be a stored procedure? What are you actually trying to achieve?
Yogi@FLG 24-Apr-18 5:10am    
Calling function is a scalar valued function where i have to evaluate a mathematical formula/expression based on some conditions using some parameters and the values to that formula are based on the parameters passed to that function. I get the formula only inside the function and after evaluating that expression i return that value .
Santosh kumar Pithani 24-Apr-18 5:44am    
Hello Yogi, you can use scalar function BY extra parameters like ex:-((@a * @b) + @c) inside your function.
Yogi@FLG 24-Apr-18 5:58am    
Hello Santhosh, that is not my exact issue i have a,b and c but how do i evaluate the expression like take your example ((@a*@b)+@c) and return that value i am replacing that a,b and c and now i want to get the value for my mathematical expression.

To be more fluent i am getting that expression ((@a*@b)+@c)within the function itself , i have a formula which i get from the parameters passed to that function , i cannot pass even the formula as for the particular conditions i may have more than one formula or expression and after i get the expression evaluated i will sum it and return that sum value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900