4

In the past I've seen use of something such as

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?

GaTechThomas
  • 783
  • 2
  • 7
  • 15

1 Answers1

9

The primary difference between your examples is that the latter does not replace 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.

SQLFox
  • 1,564
  • 12
  • 23