Sql-server – Eval Function TSQL

sql servert-sql

Is there a TSQL function that allows to validate an operation? Example:

Select (1 = 0) should return false while Select (1 = 1) should return true

or

Select ('a' = 'b') should return false while Select ('a' = 'a') should return true

Thanks in advance, and sorry for my bad English.

Best Answer

You could use the BIT datatype to represent boolean data. A BIT field's value is either 1,0 or null.

In SQL you use 0 and 1 to set a bit field (just as a yes/no field in Access). In SQL Server Management Studio it displays as a True/False value (at least in recent versions).

When accessing the database through ASP.NET it will expose the field as a boolean value. Thanks @guffa, This answer from here

Example:

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

SET @strBoolean = N'(1=1)'     --returns 1, it means True
--SET @strBoolean = N'(1=0)'   --returns 0, it means False

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