Evaluate a string Expression

  • rog pike (1/17/2009)


    Jeff Moden (1/16/2009)

    Heh... Come on folks. "We don need no stinkin' CLR!" 😛 This is a simple problem and it doesn't need a CLR.

    The Gartner group commissioned a study and with Yale medical school they

    found that the top five percent of sql experts were sqldatapaths:) Seriously,

    it's 2009 and it shouldn't be necessary to rub two sticks together to get

    a fire. There are valid reasons MS tries to quarantine it's sql experts and keep them away from developers (Linq is a type of immunization from sql type staph infections:). I find myself in agreement with this sentiment:

    "State of the UNION", January 12, 2009

    http://blog.databaseconsultinggroup.com/[/url]

    It's time to feed your head some new ideas on how to program where your

    sql expertise will serve you well. Learn to use Dataphor with sql server and you'll have the best of two worlds. And they won't collide:)

    www.beyondsql.blogspot.com

    And there are folks like yourself that think you need a Lamborgini to haul manure. Now, except for accusing you of spamming this fine site, I've not called you any names like you've just done to me. I know it's nearly impossible for you to do so, but try being a gentleman for a change. Nah... never mind... I'm expecting too much...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Create table #t (ID int, Expression nvarchar(100), Expression2 nvarchar(200) null, Resultado real null)

    insert into #t (ID, Expression)

    select 1,'8*(1/2)-6' union all

    select 2,'278*(1/4)-2' union all

    select 3,'81*(3/5) +4'

    update #t set Expression2 = 'update #t set Resultado = '+replace(Expression,'/','.0/')+' Where ID = '+convert(nvarchar(5),ID)

    Declare @ID int, @Exp nvarchar(200)

    Select @ID = min(ID) From #t

    While @ID is not null

    Begin

    Select @Exp = Expression2 From #t Where ID = @ID

    Exec (@Exp)

    Select @ID = min(ID) From #t Where ID > @ID

    End

    Select * from #t

  • If you want to evaluate more complex expression, the only easy way I know is with the SQL.NET product. It evaluates .NET expression directly in SQL stored procedure, function and trigger.

    -- CREATE expression

    DECLARE @sqlnet SQLNET = SQLNET::New('8*(1/2)-6')

    -- EVALUATE expression

    SELECT @sqlnet.Eval() -- (return: -6)quote]

    Here is the product link:

    https://github.com/zzzprojects/Eval-SQL.NET

    You can also use the .NET Runtime Compiler if you want to evaluate in your own CLR procedure:

    https://github.com/zzzprojects/Eval-Expression.NET

    Let me know if it worked for you.

  • I found this discussion because I was looking for a way to get the default value for an int column. Here is what I came up with:

    declare
    @xStr nvarchar(40)
    , @pStr nvarchar(40) = N'@dv int OUTPUT'
    , @dv int
    ;
    select @xStr = 'SELECT @dv = ' + definition from sys.default_constraints
    where object_id = OBJECT_ID(N'dbo.MyIntColumnDefaultConstraint')
    ;
    exec sp_executesql @xStr, @pStr
    , @dv = @dv OUTPUT
    ;
    select @dv [@dv], @xStr [@xStr]
    ;

     

    Sincerely,
    Daniel

Viewing 4 posts - 16 through 18 (of 18 total)

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