SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Case with Wildcards


Case with Wildcards

Author
Message
TJT
TJT
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 254
I am having trouble with a CASE statement like this.

The All Cars doesn't work.

SELECT
CASE
WHEN CARS.Model LIKE '%Ford%' THEN 'Ford'
WHEN CARS.Model LIKE '%Chev%' THEN 'Chev'
WHEN CARS.Model LIKE '%Buick%' THEN 'Buick'
WHEN CARS.Model LIKE '%' THEN 'All Cars'
END AS 'Models'
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28323 Visits: 39955
the last selection should just be an ELSE;

SELECT
CASE
WHEN CARS.Model LIKE '%Ford%' THEN 'Ford'
WHEN CARS.Model LIKE '%Chev%' THEN 'Chev'
WHEN CARS.Model LIKE '%Buick%' THEN 'Buick'
ELSE 'All Cars'
END AS 'Models'



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

TJT
TJT
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 254
The ELSE statement is not returning all models
writearjun53
writearjun53
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 227
if you want all remaining model as it is then please try this query

SELECT
CASE
WHEN CARS.Model LIKE '%Ford%' THEN 'Ford'
WHEN CARS.Model LIKE '%Chev%' THEN 'Chev'
WHEN CARS.Model LIKE '%Buick%' THEN 'Buick'
ELSE CARS.Model
END AS 'Models'
FROM CARS
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4410 Visits: 3670
Remember: if within a CASE statement the WHEN clause is TRUE, the following WHEN statements and the ELSE statement is not executed anymore.
So if some data in your example contains the value "fictive Buick Chevron" it will return "Chev" and not "Buick". Only when the data doesn't contain either "Ford" nor "Chev" nor "Buick" it will return the "All Cars" value.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
TJT
TJT
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 254
I guess I have phrased my question wrong.

What I was wondering is if it is possible to use a CASE statement to return BUICK, CHEV, and then All Models (including BUICK and CHEV)
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5008 Visits: 5478
Yes, you can. In another column:


SELECT CASE
WHEN CARS.Model LIKE '%Ford%' THEN 'Ford'
WHEN CARS.Model LIKE '%Chev%' THEN 'Chev'
WHEN CARS.Model LIKE '%Buick%' THEN 'Buick'
WHEN CARS.Model LIKE '%' THEN 'All Cars'
END AS Models
,CARS.Model
FROM yourtable


or in the same column using union:


SELECT CASE
WHEN CARS.Model LIKE '%Ford%' THEN 'Ford'
WHEN CARS.Model LIKE '%Chev%' THEN 'Chev'
WHEN CARS.Model LIKE '%Buick%' THEN 'Buick'
WHEN CARS.Model LIKE '%' THEN 'All Cars'
END AS Models
FROM yourtable
UNION
SELECT CARS.Model
FROM yourtable



One more thing: do not use single quotes around column name aliases. If you really want to enclose them in something - use square brackets, otherwise you can misread them for literal strings.:

AS 'Models' - bad
AS [Models] - good

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search