Convert string with expression to decimal

后端 未结 3 503
清歌不尽
清歌不尽 2021-01-06 16:33

I have a table which has a column \'Faktor\' (varchar(50)) which contains expressions like:

1/3
2*9/5
0.567
0.23

No, I am searching a way t

3条回答
  •  Happy的楠姐
    2021-01-06 17:02

    too bad. In the expression all numbers are int datatype. So, its difficult to calculate and show the result in decimal.

    Here is a sample, hope it will help-

    declare @expr nvarchar(10)
    set @expr='2*9/5'
    declare @query varchar(800)
    
    BEGIN TRY
        SELECT CAST(@expr as decimal(5,2))
    END TRY
    BEGIN CATCH
        SET @query='SELECT CAST('+@expr+'.0 as decimal(5,2))'
        EXECUTE(@query)
    END CATCH
    

提交回复
热议问题