Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Case with Wildcards Expand / Collapse
Author
Message
Posted Wednesday, May 15, 2013 3:31 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 7, 2014 7:04 AM
Points: 37, Visits: 103
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'
Post #1453281
Posted Wednesday, May 15, 2013 3:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 7:44 AM
Points: 12,910, Visits: 32,015
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1453283
Posted Wednesday, May 15, 2013 6:00 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 7, 2014 7:04 AM
Points: 37, Visits: 103
The ELSE statement is not returning all models
Post #1453296
Posted Thursday, May 16, 2013 12:39 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, June 5, 2014 2:51 AM
Points: 41, 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
Post #1453338
Posted Thursday, May 16, 2013 2:30 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 29, 2014 7:50 AM
Points: 2,262, Visits: 2,725
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’! **
Post #1453365
Posted Thursday, May 16, 2013 5:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 7, 2014 7:04 AM
Points: 37, Visits: 103
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)
Post #1453448
Posted Thursday, May 16, 2013 5:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1453455
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse