Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: SQL SQL-Server Formula math , +
I Wanted to store the formula text and intend to evaluate the formulae and display results of the operation..
Please help me!
Thanks a lot
Posted 18-Nov-12 5:48am
OriginalGriff at 18-Nov-12 11:24am
And? Your problem is?
What have you tried? What have you got to store so far? Where are you stuck?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

you can do this way...
store formula in sql table tblFormulas like below...
FormulaName  			FormulaEquation
Gain				S.P. - C.P.
Loss				C.P. - S.P.
Gain%				(GAIN * 100)/C.P.
Loss%			         (LOSS * 100)/S.P
Selling Price (S.P)                 ((100 + GAIN%)/100)*C.P.
Write down code in .net
replace values of parameters in equations...
for e.g.
string Equation = "((100 + GAIN%)/100)*C.P."; 
Equation = Equation.Replace("GAIN%", txtGainPer.text); // suppose txtGainPer.text = 50
Equation = Equation.Replace("C.P.", txtCP.text); // suppose txtGainPer.text = 27
Equation = Equation.Replace("S.P.", txtSP.text);
Equation = Equation.Replace("GAIN", txtGain.text);
Equation = Equation.Replace("LOSS", txtLoss.text);
//result will be like below... Equation = "((100 + 50)/100)*27";
//now send this equation to sql as below to evaluate
Equation = "Select " + Equation;
//then pass string to sql for execution...
NOTE : This code is vulnerable for sql injection
TIP: to avoid this you can use third party .net classes available for evaluate expressions OR can write code for evaluate expressions your self
Hope this helped you.
Happy Coding!
Smile | :)
Maciej Los at 23-May-14 1:56am
Welcome back Aarti!
Aarti Meswania at 23-May-14 2:58am
Thank you, Maciej Los :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If I am not wrong; you want to store some sql in a field and execute it.
Check this it may help you.
Declare @a varchar(999)
set @a='select 5*5+5'
--You can use select @a=fieldname from tablename where criterya

Exec (@a)

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

  Print Answers RSS
0 OriginalGriff 277
1 BillWoodruff 205
2 Maciej Los 205
3 DamithSL 165
4 Sergey Alexandrovich Kryukov 145
0 OriginalGriff 5,107
1 DamithSL 4,157
2 Maciej Los 3,595
3 Kornfeld Eliyahu Peter 3,450
4 Sergey Alexandrovich Kryukov 2,741

Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 23 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100