this might do it for you... though you may experience some rounding issues:CREATE FUNCTION dbo.EvalF( @eq varchar(100), @a decimal(18,10)) RETURNS decimal(18,10) AS BEGIN Set @eq = replace(@eq,'a',@a) Declare @tally table (n int) Insert Into @tally Select n = n1.n + n2.n + n3.n + n4.n + n5.n + n6.n + n7.n + n8.n From (Select n=0 Union Select 1) n1, (Select n=0 Union Select 2) n2, (Select n=0 Union Select 4) n3, (Select n=0 Union Select 8) n4, (Select n=0 Union Select 16) n5, (Select n=0 Union Select 32) n6, (Select n=0 Union Select 64) n7, (Select n=0 Union Select 128) n8 Order By n Declare @workingTable table (eq varchar(100), val1 varchar(100), val2 varchar(100), bChar varchar(5), mChar varchar(5), aChar varchar(5), st int, md int, fn int, step int, calcVal varchar(100)) Insert Into @workingTable Select eq = @eq, val1 = substring(@eq,Z.st+1,Z.fn-Z.st-1), val2 = substring(@eq,Y.st+1,Y.fn-Y.st-1), bChar = substring(@eq,Z.st,1), mChar = substring(@eq,Z.fn,1), -- bChar = substring(@eq,Y.st,1), aChar = substring(@eq,Y.fn,1), st = Z.st, md = Z.fn, fn = Y.fn, 1, null From ( Select St = A.n, Fn = min(B.n) From (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) A Left Join (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) B On A.n < B.n Group by A.n ) Z Inner Join ( Select St = A.n, Fn = min(B.n) From (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) A Left Join (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) B On A.n < B.n Group by A.n ) Y On Z.Fn = Y.St Declare @step int Set @Step = 0 While @step<=10 and exists(Select * From @workingTable Where isnull(val1,'')<>'' and isnull(val2,'')<>'' and calcVal is null) Begin Set @Step = (Select max(step) From @workingTable) Update @workingTable Set calcVal = case when mChar = '+' then convert(decimal(18,10),val1) + convert(decimal(18,10),val2) when mChar = '-' then convert(decimal(18,10),val1) - convert(decimal(18,10),val2) when mChar = '*' then convert(decimal(18,10),val1) * convert(decimal(18,10),val2) when mChar = '/' then convert(decimal(18,10),val1) / convert(decimal(18,10),val2) Else 0 end From @workingTable Where isnull(val1,'')<>'' and isnull(val2,'')<>'' and calcVal is null and step = @Step Select @eq = replace(@eq,bChar+val1+mChar+val2+aChar,calcVal) From @workingTable Where step = @step and calcVal is not null Insert Into @workingTable Select eq = @eq, val1 = substring(@eq,Z.st+1,Z.fn-Z.st-1), val2 = substring(@eq,Y.st+1,Y.fn-Y.st-1), bChar = substring(@eq,Z.st,1), mChar = substring(@eq,Z.fn,1), -- bChar = substring(@eq,Y.st,1), aChar = substring(@eq,Y.fn,1), st = Z.st, md = Z.fn, fn = Y.fn, step = @step+1, null From ( Select St = A.n, Fn = min(B.n) From (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) A Left Join (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) B On A.n < B.n Group by A.n ) Z Left Join ( Select St = A.n, Fn = min(B.n) From (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) A Left Join (Select n, chr=substring(@eq,n,1) From @tally t Where substring(@eq,n,1) in ('+','-','*','/','(',')')) B On A.n < B.n Group by A.n ) Y On Z.Fn = Y.St End Return convert(decimal(18,10),@eq)ENDTest:Declare @eq varchar(100), @a decimal(18,10)Select @eq = '(((5/9)*(a-32))/3)'Set @a = 65.78Select @eq, @a, dbo.EvalF(@eq,@a)
result:---------------------- ----------------- ----------------(((5/9)*(a-32))/3) 65.7800000000 6.2555555561(1 row(s) affected)
Corey
"If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain