patternsqlMinor
To use or not use ISNULL(NULLIF(Field1, ''))?
Viewed 0 times
nulliffield1usenotisnull
Problem
In the past I've seen use of something such as
to tersely get a fallback value.
However, since the advent of CASE in TSQL, we've favored something more like
Is one going to perform better than the other? What other reasons would there be to choose one over the other?
SELECT ISNULL(NULLIF(Field1, ''), 'NewValue') to tersely get a fallback value.
However, since the advent of CASE in TSQL, we've favored something more like
SELECT CASE Field1 WHEN '' THEN 'NewValue' ELSE Field1 END Is one going to perform better than the other? What other reasons would there be to choose one over the other?
Solution
The primary difference between your examples is that the latter does not replace
With regard to performance, you will generally need to try very hard to find a measurable difference (in the order of a few seconds over millions of rows). Search for performance measures for isnull, coalesce, and case--you'll find lots of blog posts about them.
The common voice I've heard is that you should use the structure that feels the most readable for your team, and any time you get the feeling that it might not be as performant as it could be, test it. Run it both ways, and compare the time it takes to complete, and compare the execution plans.
NULL with your default value.With regard to performance, you will generally need to try very hard to find a measurable difference (in the order of a few seconds over millions of rows). Search for performance measures for isnull, coalesce, and case--you'll find lots of blog posts about them.
The common voice I've heard is that you should use the structure that feels the most readable for your team, and any time you get the feeling that it might not be as performant as it could be, test it. Run it both ways, and compare the time it takes to complete, and compare the execution plans.
Context
StackExchange Database Administrators Q#25630, answer score: 9
Revisions (0)
No revisions yet.