SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Handling Null Values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Venkat144
Starting Member

India
2 Posts

Posted - 05/02/2012 :  12:12:27  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 05/02/2012 :  12:28:18  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
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
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/02/2012 :  12:31:32  Show Profile  Reply with Quote
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

USA
15559 Posts

Posted - 05/02/2012 :  13:05:10  Show Profile  Visit robvolk's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 05/02/2012 :  13:10:53  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 05/02/2012 :  13:12:07  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
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

Edited by - yosiasz on 05/02/2012 13:13:14
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/02/2012 :  14:10:38  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1202 Posts

Posted - 05/02/2012 :  15:58:31  Show Profile  Reply with Quote
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

Edited by - Vinnie881 on 05/02/2012 16:00:42
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 05/02/2012 :  19:37:28  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1202 Posts

Posted - 05/02/2012 :  21:19:07  Show Profile  Reply with Quote
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

Edited by - Vinnie881 on 05/02/2012 23:27:31
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3826 Posts

Posted - 05/03/2012 :  13:13:02  Show Profile  Reply with Quote
What if they have SET ANSI_NULL = OFF!!?? ;)

Edited by - Lamprey on 05/03/2012 13:13:30
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 05/04/2012 :  06:25:49  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000