'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);

sql


Solution 1:[1]

  1. There is a redundant , between capital and FROM.
  2. In SQL you use = to compare, and not ==.
  3. Looks like the site that you provided supports LEN function 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