Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Case Structure
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Nkosinathi
Starting Member

South Africa
4 Posts

Posted - 05/16/2013 :  10:09:14  Show Profile  Reply with Quote
Hello everyone,

Please assist me here, i am trying to get the appropriate description using the condition of a case structure and i am getting an error.

below is my case structure:

select case description
when devicetype_id =4 then 'Substation'
when devicetype_id =2 then 'Switching Station'
else 'Nothing'
end
from device_fr station



i am getting this error : "missing keyword at line 2"

Thank you



nknkosi@smith

djj55
Constraint Violating Yak Guru

USA
352 Posts

Posted - 05/16/2013 :  10:54:44  Show Profile  Reply with Quote
I think you might want
SELECT description = CASE when devicetype_id =4 then 'Substation'
when devicetype_id =2 then 'Switching Station'
else 'Nothing'
end 
FROM device_fr station


djj
Go to Top of Page

Nkosinathi
Starting Member

South Africa
4 Posts

Posted - 05/17/2013 :  02:32:11  Show Profile  Reply with Quote
quote:
Originally posted by djj55

I think you might want
SELECT description = CASE when devicetype_id =4 then 'Substation'
when devicetype_id =2 then 'Switching Station'
else 'Nothing'
end 
FROM device_fr station


djj



Morning djj, unfortunately im still getting the same error, it seems the sql server does not seems to find the FROM statement

nknkosi@smith
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2242 Posts

Posted - 05/17/2013 :  02:45:47  Show Profile  Reply with Quote
--See this illustration
CREATE TABLE Developers
(DeveloperID INT, ProjectCode VARCHAR(30))
INSERT INTO Developers
SELECT 1, 'CODE_PROJECT' union all
SELECT 1, 'MSFT' union all
SELECT 1, 'REPT_MOD' union all
SELECT 2, 'MSFT' union all
SELECT 2, 'CODE_PROJECT' union all
SELECT 3, 'REPT_MOD'

SELECT Description = 
		CASE WHEN DeveloperID = 1 THEN 'Result1' 
			WHEN DeveloperID = 2 THEN 'Result2' 
		 ELSE 'Nothing' END
FROM Developers

DROP TABLE Developers
Output:
Description
-----------
Result1
Result1
Result1
Result2
Result2
Nothing


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 05/17/2013 :  02:53:18  Show Profile  Reply with Quote
you could also use simple case construct if you want

SELECT  CASE devicetype_id
        when  4 then 'Substation'
        when  2 then 'Switching Station'
        else 'Nothing'
        end AS [description]
FROM device_fr station



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000