Evaluating numeric expressions

  • We have a table, which after modification finally contains numeric expression in one column like 100.0*0.12, 250*0.03+100 etc...

    The expressions contain only numbers and operators +, - , *, / and ().

    What is the simplest way of evaluating this to a result value like 12, 107.5 etc..

    Do we have equivalent of scripting languages eval function?

    Regards

    Raj

  • One alternative is to use dynamic sql.

    ie.,

    1. Write a loop to extract parts of this column separated by comma.

    say @m_expr = "250*0.03+100"

    2. To evaluate the expression use dynamic sql

    set @m_sql = "SELECT @m_result = " + @m_expr

    exec sp_executesql @m_sql, N'@m_result decimal(19,2) OUTPUT',@m_result output

    3. write code to concatenate all the @m_result values back to comma separated values

    If you are using sql*server 2000 then you can write the above as a function and call it

    in SELECT/UPDATE statement.

    If you need sample script, let me know

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

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