Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 evaluating a nvarchar expression

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-08-16 : 09:37:46
ajay writes "Here is a sample of my table which has an expression as a column.

(The expression in my original table are lot more complex and are parameterised though.)



ID (int) Expression (nvarchar data type)

1 8*(1/2)-6

2 278*(1/3)-2

3 81*(3/4) +4



But my only concern now is
I now have the expression in place as an nvarchar.(Ex:8*(1/2)-6)



Now I need to evaluate this expression and output it from a proc (evaluated output is -2 in above Ex) .

Please help with any ideas on how can I evaluate the expression which is available as a string.

I am unable to cast/convert nvarchar to float/int

This is a very urgent requirement for me.

Please get back as soon as you possible
Thanks
Ajay"

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-16 : 09:59:52
if you can do this from front end it will be great else from back end,
you need to loop through all the records and then create the dynamic sql to evalute the values

Somthing like this


Declare @var nvarchar(200)
Set @var = '8*(1/2)-6'

Exec('Select ' + @var)


Chirag
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-08-16 : 10:04:26
here is a method:


Declare @cmdStr varchar(1000),
@dataFloat float
Set @cmdStr = 'Select data = ' + @Eq

Create Table #outputFloat (data float)

Insert Into #outputFloat
Exec(@cmdStr)

Select @dataFloat = data From #outputFloat
Drop Table #outputFloat

Select @dataFloat


Corey

Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..."
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-16 : 10:08:56
[code]CREATE PROCEDURE uspEvaluate
(
@Expression NVARCHAR(100)
)
AS

exec ('select ' + @Expression + ' as Value')[/code]Also you need to store the number with a trailing ".0" for correct decimal calculation.

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -