Yep, you've got the right idea, Bob.
The way I keep it straight is this: IF is control-of-flow, ie, conditional branching. CASE is evaluation of data.
Or, IF is meta-SQL -- it's a statement that affects how SQL Server parses your code. CASE is just plain SQL, and affects how SQL Server parses your data.
If I want to choose one table or another based on a parameter, that's IF. If you want to examine a column as you're retrieving it and modify your output based on its contents, that's CASE.
(And to get even more complex, IF is a procedural statement, whereas CASE is a set-based statement. SQL is non-procedural when evaluating data. And... I'm drifting, sorry. One of my hats is SQL instructor here at work. I'll stop now.
--sofaspud
--"Listening to your kid is the audio equivalent of a Salvador Dali painting, Spud." --OpMegs
The way I keep it straight is this: IF is control-of-flow, ie, conditional branching. CASE is evaluation of data.
Or, IF is meta-SQL -- it's a statement that affects how SQL Server parses your code. CASE is just plain SQL, and affects how SQL Server parses your data.
If I want to choose one table or another based on a parameter, that's IF. If you want to examine a column as you're retrieving it and modify your output based on its contents, that's CASE.
(And to get even more complex, IF is a procedural statement, whereas CASE is a set-based statement. SQL is non-procedural when evaluating data. And... I'm drifting, sorry. One of my hats is SQL instructor here at work. I'll stop now.
--sofaspud
--"Listening to your kid is the audio equivalent of a Salvador Dali painting, Spud." --OpMegs