|
|
|
Forum 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
|
|
|
|
|
SSC 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
|
|
|
|
|
SSCertifiable
       
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
|
|
|
|
|
SSC-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
|
|
|
|
|
SSC 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
|
|
|
|
|
Forum 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
|
|
|
|