Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How Check string of Boolean expression is return true or false Expand / Collapse
Author
Message
Posted Saturday, February 09, 2013 7:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, February 10, 2013 4:26 AM
Points: 2, Visits: 4
Hi Guys,

I have a requirement in my project is how to check string of boolean expression is true or false.

Example

Declare @strBoolean nvarchar(1000)

set @strBoolean ='(1=1 AND (1=1 OR 1=1) || (1=1 AND 1=0) )

I want to check @strBoolean is true or false

ie if(@strBoolean)
print('true')
else
print('false')

Please help me to solve the problem


Thanks
Siv
Post #1418011
Posted Saturday, February 09, 2013 11:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, May 10, 2013 4:42 AM
Points: 38, Visits: 93
Hi

If @strBoolean = 1 BEGIN
Print 'True'
End else begin
If @strBoolean= 0 BEGIN
Print 'False'
End else begin
Print 'Null'
End
End

Should do it...


Time to make a change

Post #1418038
Posted Saturday, February 09, 2013 2:29 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:57 PM
Points: 6,724, Visits: 11,771
Is this SQL Server code? || is not a valid logical operator in T-SQL. Why do you want SQL Server to evaluate a passed in logic statement in this way? Surely doing that evaluation in the application language you're planning to pass it to SQL Server from would be more efficient rather than doing a roundtrip to the database.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1418051
Posted Saturday, February 09, 2013 3:25 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:29 PM
Points: 37,731, Visits: 29,997
I'd suggest doing that in application code, not database code. They're more designed for that kind of work.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1418062
Posted Sunday, February 10, 2013 12:21 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 7:19 AM
Points: 283, Visits: 1,239
You can evaluate the expression by using dynamic SQL.

There's no double-pipe in SQL. Usually X || Y means X OR Y except that Y is not evaluated if X is false. Obviously, you know the logic required so I'll just use some simplified examples.


DECLARE
@strBoolean NVARCHAR(1000),
@strSQL NVARCHAR(1000),
@bResult BIT = 0

SET @strBoolean = N'(1=1 AND (1=1 OR 2=2))' --returns 1
--SET @strBoolean = N'(1=2 AND (1=1 OR 2=2))' --returns 0

SET @strSQL = N'SELECT @bCheck = 1 WHERE '+@strBoolean
EXEC sp_executesql @strSQL, N'@bCheck INT OUT', @bResult OUT

SELECT
(CASE @bResult
WHEN 1 THEN 'True'
ELSE 'False'
END) result


Post #1418083
Posted Sunday, February 10, 2013 4:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, February 10, 2013 4:26 AM
Points: 2, Visits: 4
HI Steven Willis,


Thank you very much Steven, Your solution solved my problem.


Thanks,
Siv
Post #1418094
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse