Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SQL Server Eval Equivalent

Author  Topic 

pah4mpo
Starting Member

2 Posts

Posted - 2005-05-04 : 11:35:09
I have a script which returns a mathematical string i.e “10 + 10 “ I need a way in which to evaluate this string within an SQL Server stored procedure. I have provided some pseudo code to help illustrate my point.

Declare @income as string
SET @Income = ’10 + 10’

Set @output = eval( @income )

Print @Output.

Incoimg string is equal to : “10+10”

We need the result to return 20 ie

Eval(‘10+10’) = 20

NOT "1010" etc

Thanks in advance for your help

Paul

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-05-04 : 15:21:37
You shouldn't be doing this inside your stored procedure. Why do you have this requirement for the stored procedure instead of from your application?

Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-04 : 15:34:21
Well, that's a new one....

You would have to worry about the rules of opertaional applications (there's a real for it I forget) but for example operators get applied in an order I belive...like mult, the dicision , then addition the substraction.

Plus you'd have to worry about parathesis...and would about roots and powers..or to get crazy trig functions?

Where does EVAL come from Oracle?



Brett

8-)

EDIT:

You would have to head down this road...most likely using a function


DECLARE @expr varchar(8000)
SELECT @expr = '10+10'
SELECT CONVERT(int,
SUBSTRING(@expr,1,CHARINDEX('+',@expr)-1))
+ CONVERT(int,
SUBSTRING(@expr
, CHARINDEX('+',@expr)+1
, LEN(@expr)-CHARINDEX('+',@expr)+1
))



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-04 : 15:54:13
Aww hell...another case of overthinking a problem


DECLARE @expr nvarchar(4000), @Result int
SELECT @expr = N'10+10'

SELECT @expr = N'SELECT @Result=' + @expr
execute sp_executesql
@expr,
N'@Result int OUT', @Result OUT
SELECT @Result





Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-05-04 : 15:59:38
And don't forget to run with scissors!

Tara
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-05-04 : 16:37:08
How about writing an extended stored procedure expression validator
here are some tips: [url]http://www.arstdesign.com/articles/expression_evaluation.html[/url]
on implementation.

I think I saw a sql version at sqlservecentral.com

Or ask the users to use calc.exe

rockmoose
Go to Top of Page
   

- Advertisement -