Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Handling Null Values

Author  Topic 

Venkat144
Starting Member

2 Posts

Posted - 2012-05-02 : 12:12:27
Hi,

I have a doubt in handling null values.

my table contains values as below

SELECT 1 as no ,'John' as Fitstname, 'Brown' as MiddleName, 'Male' as Gender,GETDATE() as DOB
UNION ALL
SELECT 2,'Sara', 'Smith', 'Female',null
UNION ALL
SELECT null,'Harry', 'Thomas', NULL,GETDATE()
UNION ALL
SELECT 3,'Jennifer', 'Smith', 'Female',null
UNION ALL
SELECT null,'Jada', 'Reynolds', NULL,GETDATE()


Now I want to replace null values with empty value(0 or '') without using ISNULL , COALESCE and ANSI_NULLS ..


Thanks in advance.


Thanks & Regards
Venkat

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-05-02 : 12:28:18
why do you not want to use those functions?

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-05-02 : 12:31:32
quote:
Originally posted by Venkat144
Now I want to replace null values with empty value(0 or '') without using ISNULL , COALESCE and ANSI_NULLS ..


Then you do not want to use SQL SERVER.









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-02 : 13:05:10
quote:
Originally posted by DonAtWork
Then you do not want to use SQL SERVER.
Or it's a test for school.

Normally I wouldn't answer this one but here you go:
SELECT 1 AS NO ,'John' AS Firstname, 'Brown' AS MiddleName, 'Male' AS Gender,GETDATE() AS DOB
INTO myTable
UNION ALL
SELECT 2,'Sara', 'Smith', 'Female',NULL
UNION ALL
SELECT NULL,'Harry', 'Thomas', NULL,GETDATE()
UNION ALL
SELECT 3,'Jennifer', 'Smith', 'Female',NULL
UNION ALL
SELECT NULL,'Jada', 'Reynolds', NULL,GETDATE()

SELECT * FROM myTable

UPDATE myTable SET NO=CASE WHEN NO IS NULL THEN -1 ELSE NO END,
Firstname=CASE WHEN Firstname IS NULL THEN '???' ELSE Firstname END,
MiddleName=CASE WHEN MiddleName IS NULL THEN '???' ELSE MiddleName END,
Gender=CASE WHEN Gender IS NULL THEN '???' ELSE Gender END,
DOB=CASE WHEN DOB IS NULL THEN '1/1/1900' ELSE DOB END

SELECT * FROM myTable
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-05-02 : 13:10:53
use case statement with IS NULL

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-05-02 : 13:12:07
please let us know if you pass the test


SELECT CASE
WHEN no IS NULL THEN 0
ELSE no
END,

CASE
WHEN Fitstname IS NULL THEN ''
ELSE Fitstname
END,
CASE
WHEN MiddleName IS NULL THEN ''
ELSE MiddleName
END,

CASE
WHEN Gender IS NULL THEN ''
ELSE Gender
END,

CASE
WHEN DOB IS NULL THEN 0
ELSE DOB
END
FROM
(
SELECT 1 as no ,'John' as Fitstname, 'Brown' as MiddleName, 'Male' as Gender,GETDATE() as DOB
UNION ALL
SELECT 2,'Sara', 'Smith', 'Female',CAST(null as CHAR(15))
UNION ALL
SELECT null,'Harry', 'Thomas', NULL ,GETDATE()
UNION ALL
SELECT 3,'Jennifer', 'Smith', 'Female',null
UNION ALL
SELECT null,'Jada', 'Reynolds', NULL,GETDATE()
) a

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-05-02 : 14:10:38
This reminds me of the obfuscation contest. Didn't Peso or Brett win that one?









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-05-02 : 15:58:31
Incase you later decide you also do not want to use the word "IS" and it is also determined you do not want to use the word "Case" you can then do this:

drop table #test
SELECT *
into #test
FROM
(
SELECT 1 as no ,'John' as Fitstname, 'Brown' as MiddleName, 'Male' as Gender,GETDATE() as DOB
UNION ALL
SELECT 2,'Sara', 'Smith', 'Female',CAST(null as CHAR(15))
UNION ALL
SELECT null,'Harry', 'Thomas', NULL ,GETDATE()
UNION ALL
SELECT 3,'Jennifer', 'Smith', 'Female',null
UNION ALL
SELECT null,'Jada', 'Reynolds', NULL,GETDATE()
) a

update a
set no = 0
from #test a
where not exists (select * from #test aa where aa.no = a.no)

update a
set Gender = ''
from #test a
where not exists (select * from #test aa where aa.gender = a.gender)

update a
set dob = ''
from #test a
where not exists (select * from #test aa where aa.dob = a.dob)

select * from #test

... See how your original question makes no sense in the real world application. Use the tools that are available to you. I am not convinced this is a test question, if it is whoever designed the test should put more focus on ensuring people learn valid skills rather than hypothetical ways to do a non-real-world solution.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-02 : 19:37:28
probably it was a question to see knowledge on CASE..WHEN i guess

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-05-02 : 21:19:07
My post came off way too harsh. I was simply illustrating that the question in my opinion if it was on a test should simply have been:

"Use a case statement to handle the null values and set them to ..." rather than a question that was telling someone not to use available functions.

Re-reading the op, I guess if someone is just learning about case statements and the teacher doesn't want to say "CASE" in their question to give the test taker a hint, then they were limited with how to phrase the question.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-03 : 13:13:02
What if they have SET ANSI_NULL = OFF!!?? ;)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-04 : 06:25:49
Empty string will be interpretted differently based on the DATA TYPE
http://beyondrelational.com/modules/2/blogs/70/posts/10841/empty-string-and-default-values.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -