SQL SELECT IF

Hi Experts,

How can I perform an IF statement within SELECT statement? For example, below I am trying to get an zero if it is NULL, otherwise get the actual value:

SELECT fldAgentID, 'prod1' AS product_id,
            IIF(fldFamRateprod1 IS NULL, 0, fldFamRateprod1)
FROM sometable
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Use the ISNULL function, which tests the first value to see if it's null, and if it is replace it with the second value.
SELECT fldAgentID, 'prod1' AS product_id, ISNULL(fldFamRateprod1,0) as fldFamRateprod1
FROM sometable 

Open in new window

COALESCE() also works if you want to have more than two arguments.

CASE blocks also work if you need more custom handling.  For more reading on CASE blocks check out my image and code-heavy article called SQL Server CASE Solutions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PadawanDBAOperational DBACommented:
Try:

select
     fldAgentID,
     'prod1' as product_id,
     case
          when fldFamRateprod1 is null then 0
          else fldFamRateprod1
     end as fldFamRateprod1_scrubbed -- or whatever you want to name it
from
     sometable;     

Open in new window


Edit: Jim's is much more succinct and will actually perform better =)  And actually also made me think I should have done the original with a coalesce, which is another way to do it if you have multiple expressions and you want the first non-null value in an ordered list.  coalesce essentially creates a case =)  there have been some good EE posts around isnull vs. coalesce and isnull does perform better, it's just a T-SQL extension, not ANSI standard.  In interest of completeness, however:

select
     fldAgentID,
     'prod1' as product_id,
     coalesce( fldFamRateprod1, 0 ) as fldFamRateprod1_scrubbed -- or whatever you want to name it
from
     sometable;     

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.