October 20, 2008 at 12:21 pm
I have got a column in my table which stores mathematical formulas as strings. The base question is how do I evaluate the "string formula" to get result?
For example, I got a value like '2*5+2*5*4'.
I need a way to evaluate this string within an SQL Server stored procedure to get the result as 50
Declare @weight as string
Declare @output as numeric(18,2)
SET @weight = '2*5+2*5*4'.
Set @output = somefunction(@weight)
OutPut should be 50.
Thanks in advance for your help
October 20, 2008 at 12:43 pm
did you try this ?
Declare @weight as char(25)
Declare @output as numeric(18,2)
SET @weight = '2*5+2*5*4'
declare @TheSQL nvarchar(1000)
Set @TheSQL = 'Select @theoutput = ' + @weight
--OutPut should be 50.
exec sp_executesql @stmt = @TheSQL
, @params= N'@theoutput int OUTPUT'
, @theoutput = @output output
print @output
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
October 20, 2008 at 12:48 pm
That works, great!
Thanks a ton, ALZDBA.
October 20, 2008 at 1:09 pm
You should be aware of course that if the strings are being provided by other users, that this would be an injection target.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
October 21, 2008 at 12:49 am
Oh indeed... I did forget to mention that !
- sqlinjection risk
- if the formula contains an error .... try to figure it out.
Be aware it may start with numeric info and maybe end with textual paramertes and annotations ...
The caveot will be dynamic sql.
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
October 21, 2008 at 5:45 am
Also beware of implicit conversion
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx
Failing to plan is Planning to fail
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply