Click here to Skip to main content
15,889,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,
How to avoid null columns in select statement?
In fact in my table in a single row there are multiple columns, some are null and some are not in a single row. So how to avoid them in select. I want to do that because I am showing these data in chart, so when it selects a null value the chart suddenly goes to 0(zero). So want to avoid that.

Thanks in advance.
Posted
Comments
RedDk 3-Apr-13 12:13pm    
There's no way to chart anything using TSQL ... really. Might your custom app handle this? In TSQL I handle NULL another way using REPLACE([this],'NULL','{discontinutity}') in the SELECT clause. As I said, TSQL and "chart" does not compute considering such a void. Why NULL actually (not 0 meaning not a value)

Use COALESCE[^] function.

SQL
DECLARE @temp TABLE (Value1 INT, Value2 INT)
 
INSERT INTO @temp (Value1, Value2)
VALUES  (500, NULL)
INSERT INTO @temp (Value1, Value2)
VALUES  (1001, NULL)
INSERT INTO @temp (Value1, Value2)
VALUES  (NULL, 4002)
INSERT INTO @temp (Value1, Value2)
VALUES  (NULL, 8003)
INSERT INTO @temp (Value1, Value2)
VALUES  (8521, NULL)
INSERT INTO @temp (Value1, Value2)
VALUES (NULL, 2647)

SELECT COALESCE(Value1,0) AS Value1, COALESCE(Value2,0) AS Value2
FROM @temp


Result:
500	0
1001	0
0	4002
0	8003
8521	0
0	2647
 
Share this answer
 
SQL
DECLARE @temp TABLE (ID INT, Value1 VARCHAR(20), Value2 VARCHAR(20))

INSERT INTO @temp
        (ID, Value1, Value2)
VALUES
        (1, 'Rajan', NULL),
        (3, 'Vijayan', NULL),
        (1, NULL, 'Ravi'),
        (3, NULL, 'sudeep'),
        (2, 'kumar', NULL),
        (2, NULL, 'venkat')

SELECT DISTINCT
   ID,
   (SELECT Value1 FROM @temp t2 WHERE t2.ID = t.ID AND Value1 IS NOT NULL) AS 'Value1',
   (SELECT Value2 FROM @temp t2 WHERE t2.ID = t.ID AND Value2 IS NOT NULL) AS 'Value2'
FROM
   @temp t

Pls Refer this Example.. may this help You...Try this..
 
Share this answer
 
Comments
Avinash_Pathak 28-Mar-13 7:42am    
its still giving null value...
Member 8112455 28-Mar-13 8:01am    
use isnull() function, if null replace it with ''
eg: isnull(column_name , '')

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900