Is there an eval function for SQl Server 2005?

  • I want to pass an arithmatic expression as varchar to a function. Then I want the function to take that varchar and convert it to a arithmetic expression and solve it

    Look at code below. conversionFormula function receives values for X, Y Z and a function @expression

    Then within conversionFormula I should be able to do something like @expression.Replace(X, @valueOfX) and then after replacing

    all variables with the passed in number, the function should solve the equation and return the result

    For example

    DECLARE @expression as varchar

    @expression = 'x+y/z'

    DECLARE @valueOfX as int

    DECLARE @valueOfY as int

    DECLARE @valueOfZ as int

    @valueOfX = 2

    @valueOfY = 12

    @valueOfZ = 3

    select dbo.conversionFormula(@valueOfX @valueOfY @valueOfZ @expression) as @Result

    Anyobne know how to do this?

    Thanks

    Pete

  • I solved it, here's the result in case anyone wants it:

    DECLARE @SQLString NVARCHAR(4000)

    SET @SQLString = 'SELECT '+'X*1200'

    set @SQLString = REPLACE(@SQLString, 'X', '3')

    EXEC sp_executesql @SQLString

    We are replacing the X with the number 3 in the string and then the sp_executesql executes the expression.

    Pete

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply