Math Formula in a Variable

  • I have a scalar function to return a calculated value. In my table I have a column named LONG_FORMULA which stores a formula for calculating a new value based on dynamic data. Here is an example:

    {+}([CONTRACT_PRICE]{*}[PCP[){+}([FF&E_AMT]{*}[PCP]{*}[TAPP_CONTRACT])

    After I process the values in the formula I end up with a variable containing the actual values in a statement. example:

    +((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))

    If I perform a select on this I get an answer: 4683.463200

    But if I want to return this value from the function it will only allow me to return the statement, not the answer.

    How can I get the function to return this answer (4683.463200)? When I do something like this it produces an error: :w00t:

    DECLARE @RET_VAL DECIMAL(13,2);

    DECLARE @Formula as varchar(255);

    -- function does stuff ..

    -- resulting in @formula = '+((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))' i use a set here to abbreviate the function

    SET @Formula = '+((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))';

    SET @RET_VAL = @Formula;

    I get an error: Arithmetic overflow error converting varchar to data type numeric.

  • You can use either xml.value (but it is a problem with variables), or you could use dynamic SQL do do this.

    DECLARE @RET_VAL DECIMAL(13,2);

    DECLARE @Formula AS VARCHAR(255);

    -- function does stuff ..

    -- resulting in @formula = '+((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))' i use a set here to abbreviate the function

    SET @Formula = '+((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))';

    DECLARE @v-2 NVARCHAR(1000)

    SELECT @v-2 = 'select ' + @Formula + ' as x'

    DECLARE @t TABLE (x DECIMAL(13, 2))

    INSERT INTO @t EXEC(@v)

    SELECT TOP 1 @RET_VAL = x FROM @t

    ps: there is no function to evaluate an arithmetic expression stored in a variable in T-SQL (but you could write one in CLR).

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • Sorry, on SQL Server 2000 you cannot use table variables for inserting into, so here is a version with temporary tables:

    DECLARE @RET_VAL DECIMAL(13, 2) ;

    DECLARE @Formula AS VARCHAR(255) ;

    -- function does stuff ..

    -- resulting in @formula = '+((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))' i use a set here to abbreviate the function

    SET @Formula = '+((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))' ;

    DECLARE @v-2 NVARCHAR(1000)

    SELECT @v-2 = 'select ' + @Formula + ' as x'

    --create table #t (x DECIMAL(13, 2))

    INSERT INTO #t

    EXEC ( @v-2 )

    SELECT TOP 1

    @RET_VAL = x

    FROM #t

    DROP TABLE #t

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • Thanks for the quick reply.

    So in 2000 I should create a temp table and insert to that temp table? Since I am in a function it wont let me create or execute? I could do it outside the function, any ideas?

  • You will find that you're going to need to do that outside of a function. Exec isn't allowed in a function.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • shogg (3/7/2008)


    Thanks for the quick reply.

    So in 2000 I should create a temp table and insert to that temp table? Since I am in a function it wont let me create or execute? I could do it outside the function, any ideas?

    Oops, I missed that you need this in a function 🙁

    On 2000 I think you will not be able to do this.

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • Thanks, very helpful - much appreciated! 🙂

  • :cool::w00t::Whistling:

  • Hi

    I am actually busy doing the same thing. I was wondering if you could show me the function that maps the variables to the actual value. It would be very much appreciated

    Thanks

  • SELECT Value

    FROM Table

    WHERE Condition

    _____________
    Code for TallyGenerator

  • Hi

    Thanks for the reply but im not quite sure what you mean. I wanted to see the function that takes you from this

    {+}([CONTRACT_PRICE]{*}[PCP[){+}([FF&E_AMT]{*}[PCP]{*}[TAPP_CONTRACT])

    to this

    +((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))

    dynamically

    Thanks

  • Hi

    Thanks for the reply but im not quite sure what you mean. I was looking fo a function that took you from this

    {+}([CONTRACT_PRICE]{*}[PCP[){+}([FF&E_AMT]{*}[PCP]{*}[TAPP_CONTRACT])

    to this

    +((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))

    Dynamicaly. For many formulae at once

    Thanks

  • SELECT ([CONTRACT_PRICE]*PCP)+([FF&E_AMT]*[PCP]*[TAPP_CONTRACT])

    FROM TableWithValues

    But you're probably asking about writing a translator from one language to another.

    If that's the case then I don't see what it has to do with T-SQL.

    _____________
    Code for TallyGenerator

  • Hi

    Thanks for the reply but what i wanted to see was the function that takes you from this

    {+}([CONTRACT_PRICE]{*}[PCP[){+}([FF&E_AMT]{*}[PCP]{*}[TAPP_CONTRACT])

    to this

    +((16900.00)*(0.27))+((464.75)*(0.27)*(0.96))

    The help would be very much appreciated

    Thankyou in advance

  • You can say it as many times as you want, it doesn't make any more sense with repetition ;). Please provide sample data and an example explaining what you want, as I don't see why Sergiy's doesn't do what you're asking for. Are you trying to remove your { 's in favor of ( 's?

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

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

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