Solved

Avoid null values

Posted on 2011-03-18
3
248 Views
Last Modified: 2012-05-11
Hi,

I have databes with

AA           BB              CC          DD
5               6                 7              0

but sometimes i have this:
AA           BB              CC          DD
5               6                 0              8

I have to do this (AA+BB+CC+DD)/100

I want that SQL exclude 0 or null during dividing.

1. (AA+BB+CC)/100
2. (AA+BB+DD)/100

Thx.
0
Comment
Question by:hrvica5
3 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 35165155
You can try this:

Select (AA+BB+CC) / nullif(100, 0)

OR

use CASE statement to avoid null:

Select Case when divisor=0 then null Else (AA+BB+CC) / divisor End ,,,

In your case, 100 is divisor.

The first example is neater though
0
 
LVL 4

Accepted Solution

by:
daletian earned 500 total points
ID: 35165451
To exclude NULL, u can use the ISNULL function to substitue NULL values with what u specify

eg.ISNULL(AA, 0)  + ISNULL(BB, 0)  + ISNULL(CC, 0)  + ISNULL(DD, 0)

ISNULL(DD, 0)  wil give 0 if DD has a  NULL value

In your case, since you are dividing by a finite number, u don't need to worry about the 0s

0
 

Expert Comment

by:BlakeRogers
ID: 35165768
ISNULL ( check_expression , replacement_value )
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now