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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 My CASE Statement isn't quite converting properly
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Maverick_
Posting Yak Master

107 Posts

Posted - 03/18/2013 :  07:39:03  Show Profile  Reply with Quote
Hi guys,

I am having a small problem with the following SQL:

(CASE
WHEN action_officer.officer_name LIKE '%__' and RIGHT(action_officer.officer_name, 2) IN ('XX','XX','38') THEN RIGHT(action_officer.officer_name, 2) ELSE 'Unknown' END) as Team_Code,

(CASE (CASE
WHEN action_officer.officer_name LIKE '% __' and RIGHT(action_officer.officer_name, 2) IN ('XX','XX', '38') THEN RIGHT(action_officer.officer_name, 2) ELSE 'Unknown' END)
WHEN 'XX' THEN 'Test'
WHEN '38' THEN 'S38 - North and West'
ELSE 'Unknown'
END) as Team_Name

In the Team_Name field, the SQL is supposed translate '38' as 'Unknown' when it should really says 'S38 - North and West'.

Does anyone know what the issue could be?

shaggy
Posting Yak Master

India
245 Posts

Posted - 03/18/2013 :  07:46:08  Show Profile  Reply with Quote
Try this
(CASE
WHEN action_officer.officer_name LIKE '%[__]' and RIGHT(action_officer.officer_name, 2) IN ('XX','XX','38') THEN RIGHT(action_officer.officer_name, 2) ELSE 'Unknown' END) as Team_Code,

(CASE (CASE
WHEN action_officer.officer_name LIKE '% [__]' and RIGHT(action_officer.officer_name, 2) IN ('XX','XX', '38') THEN RIGHT(action_officer.officer_name, 2) ELSE 'Unknown' END)
WHEN 'XX' THEN 'Test'
WHEN '38' THEN 'S38 - North and West'
ELSE 'Unknown'
END) as Team_Name
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 03/18/2013 :  07:55:47  Show Profile  Reply with Quote
May be this?
CASE 
WHEN action_officer.officer_name LIKE '%[__]' and RIGHT(action_officer.officer_name, 2) IN ('XX','38') THEN RIGHT(action_officer.officer_name, 2) ELSE 'Unknown' END) as Team_Code,
CASE RIGHT(action_officer.officer_name, 2)
 WHEN 'XX' THEN 'Test'
 WHEN '38' THEN 'S38 - North and West'
 ELSE 'Unknown' END as Team_Name


--
Chandu
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 03/18/2013 :  08:02:59  Show Profile  Reply with Quote
Hi Shaggy,

Even when I test your SQL it still comes back with Unknown.
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 03/18/2013 :  08:05:51  Show Profile  Reply with Quote
Hi guys,

One thing I noticed is the action_officer.officer_name has the definition as "S38". Or in other words, it ends with S38 rather than 38. Could this cause a problem?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 03/18/2013 :  08:28:17  Show Profile  Reply with Quote
Check the following illustration once...

DECLARE @action_officer TABLE(officer_name VARCHAR(40))
INSERT INTO @action_officer VALUES('chandu'), ('gfiawaS38'), ('afbajfvXX'), ('s')
SELECT CASE WHEN RIGHT(officer_name, 2) IN ('XX','38') THEN RIGHT(officer_name, 2) ELSE 'Unknown' END as Team_Code,
	   CASE RIGHT(officer_name, 2)  WHEN 'XX' THEN 'Test'  WHEN '38' THEN 'S38 - North and West' ELSE 'Unknown' END as Team_Name
FROM @action_officer


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000