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
 General SQL Server Forums
 New to SQL Server Programming
 Cast

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-27 : 22:52:06
SELECT
CASE columnA
WHEN 'Janet' THEN columnA
ELSE columnB
END
"Name"
FROM People

I 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?

Name
Janet
Janet
Janet
Mary19
Mary19
Cloey8

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 People
WHERE 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]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-27 : 23:34:08
Mistake. It should be like this

ColumnA CloumnB
Janet Mary19
Janet Mary19
Janet Mary19
Cloey8

If is not Janet, then grab the data from columnB
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-02-27 : 23:41:30
quote:
Originally posted by peace

Mistake. It should be like this

ColumnA CloumnB
Janet Mary19
Janet Mary19
Janet Mary19
Cloey8

If 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]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-27 : 23:54:41
This is the set of data:
ColumnA CloumnB
Janet Mary19
Janet Mary19
Janet Mary19
Cloey8

What 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 nvarchar

Select
CAST(
CASE
WHEN columnA = 'Janet'
THEN columnA
ELSE columnB
END AS nvarchar(50)) as Name
From People

Or my query is wrong?
Go to Top of Page

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]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-28 : 01:19:38
It didnt give the output.

Select convert(nvarchar,columnB) from TableName

ColumnB
Mary19
Mary19
Cloey8

I need to convert?
Go to Top of Page

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]

Go to Top of Page
   

- Advertisement -