Solved

Added an IF clause to a select query

Posted on 2011-03-08
4
305 Views
Last Modified: 2012-05-11
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
Comment
Question by:mbs2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35069427
IF de.gr_Percent >0
begin
    select statement here
end
ELSE
begin
    return NULL
end
0
 
LVL 10

Expert Comment

by:Humpdy
ID: 35069436
you're looking for a CASE statement

http://www.tizag.com/sqlTutorial/sqlcase.php
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35069442
use case

SELECT case
                when de.gr_Percent > 0
                    YourField
                 ELSE
                      NULL
              end
0
 
LVL 3

Accepted Solution

by:
CarlsbergFTW earned 125 total points
ID: 35069449
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question