• imanalimi - Tuesday, August 22, 2017 8:49 PM

    Hi Guys,
    I am trying to calculate formula  using SQL Server 2016.
    How can I get the result of a Formula within a Select Statement like this:

    CREATE TABLE #tblFormula (C1 INT, C2 INT,Formula NVARCHAR(50))
    INSERT INTO #tblFormula (C1,C2,Formula)
    VALUES
    (5,1,'C1+C2')
    ,(7,2.5,'C1*C2')
    ,(4.5,8,'C1/C2');

    SELECT C1,C2,Formula,'Result' AS [Result] FROM #tblFormula

    DROP TABLE #tblFormula;

    The Result would be similar to:
    C1....C2....Formula....Result
    5........1.....C1+C2........6
    7........2.....C1*C2........14
    4........8.....C1/C2........0.5

    Many Thanks

    I do not see a way to do this within one statement however you may be able to achieve it with some procedural T-SQL. It would simple to do this in an app code language, perhaps in C# inside a SQLCLR table-valued function.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato