Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get result of a simple condition string 1

Status
Not open for further replies.

starsky51

MIS
Mar 11, 2002
91
I am trying to set up a function which takes a string containing a set of conditions and returns a bit to indicate whether the condition is true or false. Basically, I need something similar to the Javascript eval() function.

eg. INPUTSTRING='((5 = 5) AND (40 = 40))', RESULT=1
INPUTSTRING='((5 = 3) AND (40 = 40))', RESULT=0

The only way I can see of doing this is using sp_executesql, but this causes problem when included in a function.

Anyone know a simple way of doing this?
 
How about good ole if

declare @result bit

if ((5 = 5) AND (40 = 40))
set @RESULT=1
else
set @RESULT=0

print @result

 
From what I can tell you would either need to divide your string up with CHARINDEX and SUBSTRING into individual variables and evaluate it that way.

Or if you are using SS 2005 or greater you might be able to use CLR and do it in VB or C#

Simi

 
I think you're right, Simi. I've been fighting with this all day. I think I will use your suggestion of splitting the string down and evaluating each part individually.

So frustrating that it doesn't have an eval function!

Thanks.
Dave.
 
declare
@var1 as int,
@var2 as int,
@var3 as int,
@var4 as int,
@test as varchar(40),
@result as bit

set @test='((5 = 5) AND (40 = 40))'
set @var1 =substring(@test,charindex('=',@test,1)-2,1)
set @var2 =substring(@test,charindex('=',@test,1)+2,1)
print @var1
print @var2
set @var3 =substring(@test,charindex('=',@test,charindex('=',@test,1)+1)-3,2)
set @var4 =substring(@test,charindex('=',@test,charindex('=',@test,1)+1)+2,2)
print @var3
print @var4

if @var1=@var2 and @var3=@var4
set @result=1
else
set @result=0

print @result

Simi
 
Cheers Simi.
I'll have to have a think about how I can expand your example to cope with different sized numbers.

Thanks for your input.
Dave.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top