Home > Software design >  Using SQL how do I get back only the entries with the same string length from two different columns?
Using SQL how do I get back only the entries with the same string length from two different columns?

Time:04-25

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

CodePudding user response:

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

CodePudding user response:

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);
  •  Tags:  
  • sql
  • Related