100

If I say:

select max(length(Name)) 
  from my_table

I get the result as 18, but I want the concerned data also. So if I say:

select max(length(Name)), 
       Name 
  from my_table

...it does not work. There should be a self join I guess which I am unable to figure it out.

Can anyone please provide me a clue?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
JPro
  • 6,292
  • 13
  • 57
  • 83
  • 7
    I would recommend that you consider using CHAR_LENGTH() instead of LENGTH(). CHAR_LENGTH() returns the length of a string in characters. LENGTH() returns its length in bytes. For multi-byte character sets these values can be different, and you are probably concerned with character length, not byte length. – Ike Walker Mar 01 '10 at 19:17

9 Answers9

185
SELECT  name, LENGTH(name) AS mlen
FROM    mytable
ORDER BY
        mlen DESC
LIMIT 1
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
17

Edited, will work for unknown max() values:

select name, length( name )
from my_table
where length( name ) = ( select max( length( name ) ) from my_table );
cjohn
  • 11,370
  • 3
  • 30
  • 17
  • yes, but i want the concerned `name` which has maxlenght of 18 – JPro Mar 01 '10 at 17:16
  • 1
    ok I managed to get what I want like this `select max(length(Name)) as num1,Name from my_table group by Name having num1 = 18`, since I know from the first query that max is 18. But how to combine this to one query? – JPro Mar 01 '10 at 17:19
  • Ah, okay, I misread that. In MS SQL I would use select Name from my_table where length(Name) = (select max(length(Name)) from my_table), but I am fairly certain that is not correct MySQL syntax. – cjohn Mar 01 '10 at 17:20
8

Ok, I am not sure what are you using(MySQL, SLQ Server, Oracle, MS Access..) But you can try the code below. It work in W3School example DB. Here try this:

SELECT city, max(length(city)) FROM Customers;
6

In case you need both max and min from same table:

    select * from (
(select city, length(city) as maxlen from station
order by maxlen desc limit 1)
union
(select city, length(city) as minlen from station
order by minlen,city limit 1))a;
Suman
  • 476
  • 5
  • 7
2

Use:

  SELECT mt.name 
    FROM MY_TABLE mt
GROUP BY mt.name
  HAVING MAX(LENGTH(mt.name)) = 18

...assuming you know the length beforehand. If you don't, use:

  SELECT mt.name 
    FROM MY_TABLE mt
    JOIN (SELECT MAX(LENGTH(x.name) AS max_length
            FROM MY_TABLE x) y ON y.max_length = LENGTH(mt.name)
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
2
Select URColumnName From URTableName Where length(URColumnName ) IN 
(Select max(length(URColumnName)) From URTableName);

This will give you the records in that particular column which has the maximum length.

Jujhar Singh
  • 3,641
  • 5
  • 29
  • 38
2

Use CHAR_LENGTH() instead-of LENGTH() as suggested in: MySQL - length() vs char_length()

SELECT name, CHAR_LENGTH(name) AS mlen FROM mytable ORDER BY mlen DESC LIMIT 1

Rajesh Goel
  • 3,277
  • 1
  • 17
  • 13
  • in my case, I want to know if datas in mediumtext could be copied in tinytext field so `length` is what I need ! – bcag2 Apr 29 '21 at 11:40
1
select * 
from my_table 
where length( Name ) = ( 
      select max( length( Name ) ) 
      from my_table
      limit 1 
);

It this involves two table scans, and so might not be very fast !

lexu
  • 8,766
  • 5
  • 45
  • 63
Martin
  • 9,674
  • 5
  • 36
  • 36
  • The limit in the sub-query is unnecessary: max() is an aggregation operator and will only return 1 row. – Martin Mar 01 '10 at 19:37
0

I suppose you could use a solution such as this one :

select name, length(name)
from users
where id = (
    select id
    from users
    order by length(name) desc
    limit 1
);

Might not be the optimal solution, though... But seems to work.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663