Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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 Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, May 22, 2016 9:04 AM
Points: 90, 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'
Post #1453281
Posted Wednesday, May 15, 2013 3:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:34 AM
Points: 14,330, Visits: 37,431
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!
Post #1453283
Posted Wednesday, May 15, 2013 6:00 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, May 22, 2016 9:04 AM
Points: 90, Visits: 254
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, May 27, 2016 4:42 AM
Points: 2,851, Visits: 3,337
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 Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, May 22, 2016 9:04 AM
Points: 90, 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)
Post #1453448
Posted Thursday, May 16, 2013 5:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, April 29, 2016 8:08 AM
Points: 2,934, Visits: 5,473
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