Click here to Skip to main content
15,895,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a table like as i mentioned below.

VB
COL A
10
NULL
20
NULL
40
20
NULL


i wanted to output like as i mentioned below can you please help me on this.
VB
Col A
20
10
30
10
50
30
10
Posted

ISNULL() can help you in this regard.
Try this
SQL
SELECT A,ISNULL(A,0)+10 AS B
FROM YourTableName

Example:
SQL
SELECT A,ISNULL(A,0)+10 AS B
FROM 
(
	SELECT 10 AS A
	UNION ALL
	SELECT NULL
	UNION ALL
	SELECT 20
	UNION ALL
	SELECT NULL
	UNION ALL
	SELECT 40
	UNION ALL
	SELECT 20
	UNION ALL
	SELECT NULL
) AS T

Output:
A	    B
------------------
10	     20
NULL	 10
20	     30
NULL	 10
40	     50
20	     30
NULL	 10


Hope, it helps :)
 
Share this answer
 
v3
Comments
Maciej Los 14-Mar-15 9:34am    
Great! +5!
Please, see my solution ;)
Another way is to use COALESCE function[^]

SQL
SELECT COALESCE(ColA, 0) + 10 AS ColB
FROM TableName


In case you want to update your table (replace existing values), use this:

SQL
UPDATE t1 SET t1.ColA = COALESCE(t2.ColA, 0) + 10
FROM TableName AS t1 INNER JOIN TableName AS t2 ON t1.PK = t2.PK


where PK means: Primary Key
 
Share this answer
 
v2

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