473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

execute formula stored as string

3
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
Oct 20 '08 #1
1 9392
ck9663
2,878 Expert 2GB
Two ways:

1. Use sp_executesql and use it's ability to return a parameter.


2. Take the long way:

Expand|Select|Wrap|Line Numbers
  1. declare @strFormula varchar(150), @intLength int, @inWidth int, @intHeight int, @intResult int
  2.  
  3. set @strFormula = 
  4.     '
  5.     declare @FormulaResult int
  6.     set @FormulaResult = [Length] * [Width] * [Height]
  7.     select @FormulaResult as FormulaResult
  8.     '
  9.  
  10. select  @intLength = 2, @inWidth = 5, @intHeight = 3
  11.  
  12. set @strFormula = replace(replace(REPLACE(@strFormula,'[Length]',cast(@intLength as varchar(3))),'[Width]', cast(@inWidth as varchar(3))),'[Height]',CAST(@intHeight as varchar(3)))
  13.  
  14. select @strFormula as strFormula
  15.  
  16. exec  (@strFormula)
  17.  
  18.  
Good luck!

-- CK
Oct 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: salil bhole | last post by:
How do you use sqldmo object to execute system stored procedures? Any suggestions would be appreciated. *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in...
2
by: Duane Phillips | last post by:
How do you execute a stored procedure in MS SQL Server? I can design and execute them from MS Access dev front end, but cannot seem to find how to run them in the Enterprise Manager. TIA. ~...
1
by: FireListen | last post by:
Hello all, Is there anyone who can tell me how to execute a stored procedure in .net. More detailed, I want to pass some parameters to the procedure and the procedure must return some value in the...
4
by: Klaus Jensen | last post by:
Hi! I am currently converting an ASP/SQL Server application to VB.Net/Access. The application performs calculations based on which products are selected. Therefore i need to be able to store...
2
by: Mike TI | last post by:
April 20, 2006 Hi All Thank you every one for your help, I have now started to get the feel of VB.Net 2005. How can I execute something stored in a variable. For example, I want to store:
1
by: Nishanth | last post by:
Hi Can someone give me the code on how to execute a Stored Procedure in c++. Thanks Nishanth
0
by: mongolian | last post by:
Hello, I am trying to execute an Extended Stored Procedure in a User Defined Function I wrote. It really is quite simple but I get an error saying "Only functions and extended stored procedures...
1
by: PVBHANU | last post by:
Hi, I am using DB2 V9.1 windows , can any one please tell me how to complile and execute a stored procedure. I followed Alldatabse->database->Application Object->stored procedure...But No...
1
by: Karthik C | last post by:
i am using DB2 database and successfully opened a db2 database connection in javascript. need a sample statement of execute the stored procedures from javascript.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.