How do I find the field with the longest length of a specific column in a MySQL table?
Find longest string in column. To find the longest string (name, word, etc.) in a column, you can use an array formula based on INDEX and MATCH, together with LEN and MAX. In the example shown, the formula in F6 is: Where "names" is the named range C5:C14. Note: this is an array formula and must be entered with control + shift + enter.
Find the max length in a column. Here is a formula that can quickly find the max length in a column. Select a blank cell which will output the result, type this formula =MAX (LEN (A1:A63)) (A1:A63 is the range you use if you want to use a column, change it to A:A), and press Shift+ Ctrl + Enter keys together. See screenshot:
To find the longest string in a range with criteria, you can use an array formula based on INDEX, MATCH, LEN and MAX. In the example shown, the formula in F6 is: { = INDEX ( names , MATCH ( MAX ( LEN ( names ) * ( class = F5 )), LEN ( names ) * (...
Now let’s find the shortest and longest firstName from the table we have just created using char_length (), min (), and max () functions and LIMIT clause. Use the ow syntax to find the shortest firstName in the friends table:
MySQL has a lot of string functions you can use:
SELECT LENGTH(col) as my_len FROM my_table ORDER BY my_len DESC LIMIT 1
More funky version (it works):
SELECT MAX(LENGTH(col)) FROM my_table
You can use the mysql command LENGTH()
<?php
$q = mysql_query("SELECT LENGTH(yourfield) AS fieldlength FROM yourtable ORDER BY fieldlength DESC LIMIT 1");
echo $longestfield = mysql_result($q,0);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With