Home > Back-end >  LIKE Function in SQL Server
LIKE Function in SQL Server

Time:09-06

Query the list of CITY names from the STATION table that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.

https://www.hackerrank.com/challenges/weather-observation-station-12/problem

Using the LIKE operator I came up with 3 queries as follows :

Working

SELECT DISTINCT CITY 
FROM STATION 
WHERE CITY NOT LIKE '[aeiou]%' 
  AND CITY NOT LIKE '%[aeiou]'

Working

SELECT DISTINCT CITY 
FROM STATION 
WHERE CITY LIKE '[^aeiou]%[^aeiou]';

Not working:

SELECT DISTINCT CITY 
FROM STATION 
WHERE CITY NOT LIKE '[aeiou]%[aeiou]';

Can someone tell why this third query is not working?

CodePudding user response:

The third approach finds cities that don't start and end with a vowel. It will incorrectly return cities that start with a vowel but don't end with one, or cities that don't start with a vowel but do end with one.

CodePudding user response:

All your answers are incorrect! This is it:

select DISTINCT CITY from STATION where CITY LIKE '[^aeiouAEIOU]%[^aeiouAEIOU]';

  • Related