Since it is an int, you should probably change the code to:
[tt]
Where batch is not null
[/tt]
With the code the way you had it, there was a lot of inefficiencies, and clearly an error.
You see, IsNull returns the value of the first parameter if the value is NOT NULL, otherwise it returns the value of the 2nd parameter.
You must also consider the fact that SQL Server will automatically do data type conversions for you. The IsNull function can take any data types as parameters, but will only return a single data type. If both parameters are integers, the output is integer. If both parameters as varchar, the output is varchar. If the data types are different, IsNull is [google]SQL Server Data Type Precedence[/google] to determine the output data type. In this case, you have an integer and a hard coded string. The integer has a higher precedence so the string will be converted to an integer. SQL Server will convert an empty string to the integer value 0. As you can see from this:
So, when you have:
[tt]Where IsNull(IntegerColumn, '') <> ''[/tt]
This is exactly the same as:
[tt]Where IsNull(IntegerColumn, 0) <> 0[/tt]
Now take a close look at what is going on here. Clearly we have a where clause which is filtering out the rows to return. The only rows we will get back from the query are rows with an actual value and that actual value is not 0.
The interesting thing is that the code shown above is equivalent to:
[tt]Where IntegerColumn <> 0[/tt]
They are equivalent because NULL <> 0 so you will still get the same result set.
If you don't want to remove the rows that have a value of 0, then you should use this:
If you also want to remove the rows where the value of batch is 0, then use this:
Let's think about the original code again but with the context of strings.
[tt]Where IsNull(VarCharColumn, '') <> ''[/tt]
Apparently, the intent is to only return rows with actual values. The same can be accomplished with this:
[tt]Where VarCharColumn > ''[/tt]
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom