简体   繁体   中英

Case expression “missing keyword” error

I've got the following table:

City     | Inhabitants | Magnitude
---------+-------------+--------
Zuerich  |  100000     | city
London   | 5000000     | city
New York | 5000000     | city

I want a read out a select query that if there are 5000000 inhabitants it has to give out "big city" like this:

City     | Inhabitants | Magnitude
---------+-------------+---------
Zuerich  |  100000     | city
London   | 5000000     | bigcity
New York | 5000000     | bigcity

I've tried it with this case statement but it didn't work

SELECT
    CASE magnitude
       WHEN inhabitants LIKE '%5000000%'
          THEN 'bigcity'
       ELSE NULL
    END AS magnitude

I always get a "missing keyword" error.

How can I write this query correctly?

Why do you use LIKE ? is it a number? can it be 050000000? or 500000005? if not the LIKE is unnecessary and normal comparison should be used.

Also, ELSE null is not correct, since it seems you want the alternative to be city

SELECT t.city,
       t.inhabitants,
       CASE WHEN t.inhabitants = '5000000' THEN 'bigcity'
                                           ELSE t.magnitude
       END as magnitude
FROM YourTable t

If my assumptions are wrong, replace my comparison with the LIKE like you used it.

Your CASE syntax is bit wrong. It should be

case when <condition> then <value> else <default> end

So for your case it should be

select case 
 when inhabitants like '%5000000%' then 'bigcity'
else 'City' end as Magnitude

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM