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 |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2006-07-16 : 03:39:02
|
| i have an sql statementselect participantid,lastname +',' +wifesname as cname from participants order by lastname,firstnamenow the problem is if wifesname is blank then it shows a blank value as cname. I would like cname to just show the lastname if wifesname is blank -- can i do that in an sql select statement |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-07-16 : 04:45:56
|
Somthing like this select participantid,lastname +',' +Case When wifesname = '' or wifesname Is Null Then ' CName' Else wifesname End as cname from participants order by lastname,firstname Chirag |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-16 : 23:16:19
|
"now the problem is if wifesname is blank then it shows a blank value as cname"by blank do you mean empty string or NULL ? If column wifesname is an empty string, lastname + ',' + wifesname will not be blank but having value of lastname + ','.Most probably your case of wifesname having NULL value. You can use ISNULL or COALESCE to handle this.select participantid, coalesce(lastname, lastname + ',' + wifesname) as cname from participants order by lastname, firstname KH |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-17 : 06:58:11
|
Here's my effort.--datadeclare @participants table (participantid int identity(1, 1), lastname varchar(20), firstname varchar(20), wifesname varchar(20))insert @participants select 'Smith', 'John', 'Sarah'union all select 'Jones', 'Dave', ''union all select 'Williams', 'Fred', nullunion all select 'Cruise', 'Tom', ' '--calculationselect participantid, lastname + isnull(', ' + nullif(wifesname, ''), '') as cnamefrom @participantsorder by lastname, firstname/*resultsparticipantid cname ------------- ------------------------------------------ 4 Cruise2 Jones1 Smith, Sarah3 Williams*/Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|