sql server 2008 - SQL arithmetic with variable operator -
i have query generates results of following fldnum float field, fldop nvarchar field returns values of either '/' or '*', , fldcalc varchar field contains numbers; when attempt execute won't work @ however, error of nvarchar float conversion error...
([fldnum] + [fldop] + convert(float,[fldcalc])) example data if fldop '/' (0.5533/34)
i used case statement such following, works fine...
(case when [fldop] = '/' ([fldnum]/convert(float,[fldcalc])) else ([fldnum]*convert(float,[fldcalc])) end)
i need more dynamic in case need add + or - fldoperator field. there anyway of doing this?
you try dynamic query. following gives idea
declare @num1 float declare @num2 float declare @op nvarchar declare @ssql nvarchar(500) set @num1 = 0.5533 set @num2 = 34.0 set @op = '/' set @ssql = 'select ' + cast(@num1 nvarchar(255)) + @op + cast(@num2 nvarchar(255)) exec sp_executesql @ssql
depending on set operator (@op) operation is. above gives 0.0162735 result.
Comments
Post a Comment