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.
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 belowSELECT 1 as no ,'John' as Fitstname, 'Brown' as MiddleName, 'Male' as Gender,GETDATE() as DOBUNION ALLSELECT 2,'Sara', 'Smith', 'Female',nullUNION ALLSELECT null,'Harry', 'Thomas', NULL,GETDATE()UNION ALLSELECT 3,'Jennifer', 'Smith', 'Female',nullUNION ALLSELECT 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 & RegardsVenkat |
|
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 |
 |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-05-02 : 12:31:32
|
quote: Originally posted by Venkat144Now 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.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-05-02 : 13:05:10
|
quote: Originally posted by DonAtWorkThen 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 DOBINTO myTableUNION ALLSELECT 2,'Sara', 'Smith', 'Female',NULLUNION ALLSELECT NULL,'Harry', 'Thomas', NULL,GETDATE()UNION ALLSELECT 3,'Jennifer', 'Smith', 'Female',NULLUNION ALLSELECT NULL,'Jada', 'Reynolds', NULL,GETDATE()SELECT * FROM myTableUPDATE 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 ENDSELECT * FROM myTable |
 |
|
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 |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-05-02 : 13:12:07
|
please let us know if you pass the testSELECT 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 DOBUNION ALLSELECT 2,'Sara', 'Smith', 'Female',CAST(null as CHAR(15))UNION ALLSELECT null,'Harry', 'Thomas', NULL ,GETDATE()UNION ALLSELECT 3,'Jennifer', 'Smith', 'Female',nullUNION ALLSELECT null,'Jada', 'Reynolds', NULL,GETDATE()) a <><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
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.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
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 #testSELECT * into #test FROM(SELECT 1 as no ,'John' as Fitstname, 'Brown' as MiddleName, 'Male' as Gender,GETDATE() as DOBUNION ALLSELECT 2,'Sara', 'Smith', 'Female',CAST(null as CHAR(15))UNION ALLSELECT null,'Harry', 'Thomas', NULL ,GETDATE()UNION ALLSELECT 3,'Jennifer', 'Smith', 'Female',nullUNION ALLSELECT null,'Jada', 'Reynolds', NULL,GETDATE()) aupdate aset no = 0from #test awhere not exists (select * from #test aa where aa.no = a.no)update aset Gender = ''from #test awhere not exists (select * from #test aa where aa.gender = a.gender)update aset dob = ''from #test awhere 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 |
 |
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
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 |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-05-03 : 13:13:02
|
What if they have SET ANSI_NULL = OFF!!?? ;) |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|