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 |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-02-27 : 22:52:06
|
| SELECTCASE columnA WHEN 'Janet' THEN columnA ELSE columnB END"Name"FROM PeopleI would like to get the output if is not Janet.I tried to cast to nvarchar but it return empty.How can I cast this?NameJanetJanetJanetMary19Mary19Cloey8 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-02-27 : 23:26:08
|
you current query does not make sense. You are returning the result as it is without changes.I would like to get the output if is not Janet.SELECT *FROM PeopleWHERE columnA <> 'Janet'I tried to cast to nvarchar but it return empty.What column is this ? What is the data type of this column ?Example ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-02-27 : 23:34:08
|
| Mistake. It should be like thisColumnA CloumnBJanet Mary19Janet Mary19Janet Mary19 Cloey8If is not Janet, then grab the data from columnB |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-02-27 : 23:41:30
|
quote: Originally posted by peace Mistake. It should be like thisColumnA CloumnBJanet Mary19Janet Mary19Janet Mary19 Cloey8If is not Janet, then grab the data from columnB
What is that ? Your data or the required result ?Can you post your sample data and the corresponding expected result ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-02-27 : 23:54:41
|
| This is the set of data:ColumnA CloumnBJanet Mary19Janet Mary19Janet Mary19 Cloey8What i want to get is, if the data is not Janet from columnA, then grab from columnB which is Mary19, Cloey8, etc. (depends on the where condition).But my result is, it didnt appear the Mary19. I have convert it to nvarcharSelectCAST( CASE WHEN columnA = 'Janet' THEN columnAELSE columnBEND AS nvarchar(50)) as NameFrom PeopleOr my query is wrong? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-02-28 : 01:09:01
|
[code]select case wehen ColumnA <> 'Janet' then ColumnB else ColumnA end[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-02-28 : 01:19:38
|
| It didnt give the output.Select convert(nvarchar,columnB) from TableNameColumnBMary19Mary19Cloey8I need to convert? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-02-28 : 01:48:36
|
why do you need to convert ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|