>> I'm pretty confident that this will do the trick regardless of what data type the field is.
I do not agree with you. Let me explain....
If your data type is varchar (or any other type of string), your code will return rows where the value of the column is NULL or an empty string. So, you're probably thinking, "It works, so I'm done." Unfortunately, working, and
working fast are completely separate. Your code is not [google]sargable[/google], which means that it will perform slower than it needs to. Often times, you can re-write a non-sargable query in to a sargable query. Like this...
Code:
[COLOR=blue]SELECT[/color] CM.ControlID, CM.LoanNumber
[COLOR=blue]FROM[/color] tblControlMaster CM
[COLOR=blue]WHERE[/color] (LoanNumber [COLOR=blue]Is[/color] NULL Or LoanNumber = [COLOR=red]''[/color])
[COLOR=blue]ORDER[/color] [COLOR=blue]BY[/color] CM.ControlId
If your table has an index on LoanNumber, the query I just showed you will use the index to identify the records faster than your query will. With small tables, you may not notice a difference in performance, but as the table grows, it will become more obvious.
Generally speaking, it is better for performance if you do NOT use any functions or calculations on columns when you can avoid it. In your query, you used the NULLIF function. In k01's query, the IsNull function was used. By separating the where clause condition in to 2 conditions (like I showed), there is no function on the column.
Alternatively, you could use the knowledge that NULL values do not compare to other values and construct your query like this...
Code:
[COLOR=blue]SELECT[/color] CM.ControlID, CM.LoanNumber
[COLOR=blue]FROM[/color] tblControlMaster CM
[COLOR=blue]WHERE[/color] LoanNumber > [COLOR=red]''[/color]
[COLOR=blue]ORDER[/color] [COLOR=blue]BY[/color] CM.ControlId
You will get the same results, and the query will execute faster.
Now, in your original post, you mentioned other data types. All the queries so far operate as though the data type is a string. This will NOT work for all data types, but will work for some.
Ex:
Code:
Declare @Temp [!]int[/!]
Set @Temp = 0
Select 1
Where @Temp = ''
This appears to work, but only because SQL Server converts an empty string to 0.
Code:
Declare @Temp [!]Numeric(10,2)[/!]
Set @Temp = 12.34
Select 1
Where @Temp = ''
[red]Error converting data type varchar to numeric.[/red]
Code:
Declare @Temp [!]UniqueIdentifier[/!]
Set @Temp = NULL
Select 1
Where @Temp = ''
[red]Syntax error converting from a character string to uniqueidentifier.[/red]
As you can see, this code does not work for all data types.
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom