Added an IF clause to a select query

I'm using a select and insert statement to insert data and need to use an IF statment to do the following:

Select the field if it is >0 otherwise return and insert a null value, first attempt which broke the script is below!!!:

SELECT IF de.gr_Percent >0 ELSE return NULL
mbs2000Asked:
Who is Participating?
 
CarlsbergFTWConnect With a Mentor Commented:
you might want to try this query:

SELECT CASE de.gr_Percent WHEN > 0 THEN de.gr_Percent ELSE null END
FROM YOUR_TABLE_HERE
0
 
Lee SavidgeCommented:
IF de.gr_Percent >0
begin
    select statement here
end
ELSE
begin
    return NULL
end
0
 
HumpdyCommented:
you're looking for a CASE statement

http://www.tizag.com/sqlTutorial/sqlcase.php
0
 
Ephraim WangoyaCommented:
use case

SELECT case
                when de.gr_Percent > 0
                    YourField
                 ELSE
                      NULL
              end
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.