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 2008 Forums
 Transact-SQL (2008)
 Case Structure

Author  Topic 

Nkosinathi
Starting Member

4 Posts

Posted - 2013-05-16 : 10:09:14
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

352 Posts

Posted - 2013-05-16 : 10:54:44
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

4 Posts

Posted - 2013-05-17 : 02:32:11
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-17 : 02:45:47
--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

52326 Posts

Posted - 2013-05-17 : 02:53:18
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
   

- Advertisement -