When i run this query i expect the result will be 'false'
IF isnull(0,'') = ''
select 'true'
else
select 'false'
But sql Server tells me 'true', why?
In this case ISNULL(0,'') returns an integer. SQL Server will cast the second argument to an integer too, ie 0. So 0=0, hence the result is TRUE. Comparing directly to 0 would also return true:
IF 0 = ''
select 'true'
else
select 'false'
Using ISNULL and NULL like this is unusual. An ISNULL(someColumn='') function in a WHERE clause would prevent the optimizer from using any indexes that covered someColumn thus forcing a scan instead of an index seek.
Using IF statements in SELECT is impossible. Even in CASE statements, it's better to explicitly check for NULL than apply such transformations.
IF ISNULL construct? Most likely, there are easier and faster ways to do itFor your case, when compare two values, the '' will be converted to int first. The following:
SELECT CONVERT(INT, '')
returns 0, so 0=0 is true
If you want treat 0 as NULL, you can use NULLIF:
DECLARE @i INT = 0
IF NULLIF(ISNULL(@i, ''), 0) = ''
SELECT 'true'
ELSE
SELECT 'false'
This would return 'false'
ISNULL()like this? Using such code in aWHEREorSELECTstatement to convert null values to something else would force a full table scan. Using this in aSELECTstatement isn't possible