'Using SQL how do I get back only the entries with the same string length from two different columns?
I have a name(the country) and a capital column inside my world table. I want to show only the name and capital that have the same number of characters. Like Greece and Athens that both equal 6 characters. I'm trying to use the LENGTH function, but unsure of what I'm doing wrong.
SELECT name, capital FROM world WHERE LEN(name) = LEN(capital);
Solution 1:[1]
- There is a redundant
,betweencapitalandFROM. - In SQL you use
=to compare, and not==. - Looks like the site that you provided supports
LENfunction in order to get the length of a string.
SELECT name, capital FROM world WHERE LEN(name) = LEN(capital);
Solution 2:[2]
You should not put comma(,) in front of "FROM" clause and you should use one equal to "=".
This should works -
SELECT name, capital FROM world WHERE LENGTH(name) = LENGTH(capital);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | idanz |
| Solution 2 |
