Solved

Added an IF clause to a select query

Posted on 2011-03-08
4
290 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
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:ewangoya
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Where not exists in same table 3 68
how many extra RAM for SQL server is needed 22 39
Table create permissions on SQL Server 2005 9 41
Help with simplifying SQL 6 53
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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