Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL Puzzle | The Tricky NULL Puzzle – 1

In this puzzle you have to replace the NULL with singlequotes(”) without using ISNULL and coalesce functions. You also cannot use CASE Condtion. For more details please check the sample input and expected output.

Sample Input

Id
a
NULL
c

Expected Output

Id
a
c

Script

Use below script to create table and insert sample data into it.

--



CREATE TABLE HandleNulls1
(
   Id VARCHAR(10)  
)
GO

INSERT INTO HandleNulls1 VALUES
('a'),
(NULL),
('c')


--

Rules/Restrictions

The solution should be should use “SELECT” statement or “CTE”.

Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

Solution – 1

--

SELECT CONCAT(Id,NULL) Id FROM HandleNulls1


--

Output-1

--

 
/*------------------------
SELECT CONCAT(Id,NULL) Id FROM HandleNulls1
------------------------*/
Id
----------
a

c

(3 row(s) affected)


--

Solution – 2 | Another one with IIF

--

SELECT IIF(Id IS NULL,'',Id) FROM HandleNulls1


--

Output-2

--

 
/*------------------------
SELECT IIF(Id IS NULL,'',Id) FROM HandleNulls1
------------------------*/

----------
a

c

(3 row(s) affected)



--

Other Reference Puzzles – The NULL Puzzles

1 https://msbiskills.com/2017/02/15/sql-puzzle-the-isnull-puzzle/
2 https://msbiskills.com/2016/07/26/sql-puzzle-remove-nulls-via-row-wise-pattern/
3 https://msbiskills.com/2016/05/18/sql-puzzle-the-null-columns-puzzle/
4 https://msbiskills.com/2016/02/07/sql-puzzle-the-remove-null-puzzle/
5 https://msbiskills.com/2015/08/06/t-sql-query-the-remove-nulls-puzzle/
6 https://pawankkmr.wordpress.com/2015/05/10/t-sql-query-the-previous-valuenon-null-puzzle/
7 https://pawankkmr.wordpress.com/2015/04/02/t-sql-query-count-null-values-puzzle/
8 https://pawankkmr.wordpress.com/2015/03/31/t-sql-query-group-by-remove-null-uom-puzzle/
9 https://pawankkmr.wordpress.com/2012/07/24/t-sql-6/

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Advertisements