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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case function

Author  Topic 

IBoonZ
Yak Posting Veteran

53 Posts

Posted - 2009-03-20 : 05:47:10
Hi

I have following collumns

Suite_ID/ Product_ID / Suiteproduct / Name Suite /

1 // 10 // 1_10 // suite1
2 // 15 // 2_15 // Suite2
3 // 20 // 3_20 // Suite 3
-1 // 25 // -1_25 // NULL

I want in the Name_Suite Collumn that when Suite_ID = -1
the Name_suite Collumn says 'NO_SUITE'.

I tried

Case when Suite_Id = '-1' THEN Name_suite = 'No Suite Available' else NULL end as Name_suite.

But he gives errors :(.

Thanks :)

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2009-03-20 : 05:53:04
Try with this.

select Case Suite_Id when -1 THEN 'NO_SUITE' else Name_suite end as Name_suite from table_name

If it still gives error. post the error.


Karthik
Go to Top of Page

IBoonZ
Yak Posting Veteran

53 Posts

Posted - 2009-03-20 : 06:01:54
he :).
No error here. Thx for the help
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-20 : 06:19:48
In this statement
Case when Suite_Id = '-1' THEN Name_suite = 'No Suite Available' else NULL end as Name_suite.

What's the error u got , just Post that error Once.
Go to Top of Page

IBoonZ
Yak Posting Veteran

53 Posts

Posted - 2009-03-20 : 06:31:11
Incorrect syntax near '='.
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-20 : 06:54:57
This is also work for u

Case when Suite_Id = '-1' THEN Name_suite = 'No Suite Available' else NULL end as Name_suite.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-20 : 08:47:52
Case when Suite_Id = '-1' THEN 'No Suite Available' else NULL end as Name_suite

or Name_suite = Case when Suite_Id = '-1' THEN 'No Suite Available' else NULL end

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-20 : 09:42:02
this will also do job for you
SELECT COALESCE(NULLIF(Name_suite,-1),'NO_SUITE') FROM table_name
Go to Top of Page
   

- Advertisement -