Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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
0
mbs2000
Asked:
mbs2000
1 Solution
 
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
 
CarlsbergFTWCommented:
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

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now