Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find longest string in the table column data

Tags:

sql

ms-access

I've a table contains the columns like

  Prefix    |  CR ----------------------------------------   g         |  ;#WR_1;#WR_2;#WR_3;#WR_4;#    v         |  ;#WR_3;#WR_4;#   j         |  WR_2   m         |  WR_1   d         |  ;#WR_3;#WR_4;#      f9        |  WR_3 

I want to retrieve data from CR column WHERE it has the longest text string i.e in current table it is ;#WR_1;#WR_2;#WR_3;#WR_4;#. I'm using

SELECT max(len(CR)) AS Max_Length_String FROM table1  

But it retuns

Max_Length_String ---------------------------------------- 26 

But what i need is not the length (26), i wanted like this

Max_Length_String ---------------------------------------- ;#WR_1;#WR_2;#WR_3;#WR_4;#  
like image 247
vuyy1182 Avatar asked Feb 19 '14 15:02

vuyy1182


People also ask

How do you find the longest string value in a column in SQL?

SELECT TOP 1 * FROM friends WHERE len(firstName) = (SELECT min(len(firstName)) FROM friends);

How do you find the length of a string in a column in SQL?

LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data. Returns : It returns the number of characters of an input string, excluding the trailing spaces.

How do you find the longest value in a column in Excel?

=MAX(LEN(C1:C2886)) Replace the C1:C2886 with your column and number of rows, then press Ctrl+Shift+Enter. It's very important you don't just press Enter, it won't work. The cell will now show the maximum length of the largest value for the column!


2 Answers

The easiest way is:

select top 1 CR from table t order by len(CR) desc 

Note that this will only return one value if there are multiple with the same longest length.

like image 90
Gordon Linoff Avatar answered Oct 18 '22 13:10

Gordon Linoff


You can:

SELECT CR  FROM table1  WHERE len(CR) = (SELECT max(len(CR)) FROM table1) 

Having just recieved an upvote more than a year after posting this, I'd like to add some information.

  • This query gives all values with the maximum length. With a TOP 1 query you get only one of these, which is usually not desired.
  • This query must probably read the table twice: a full table scan to get the maximum length and another full table scan to get all values of that length. These operations, however, are very simple operations and hence rather fast. With a TOP 1 query a DBMS reads all records from the table and then sorts them. So the table is read only once, but a sort operation on a whole table is quite some task and can be very slow on large tables.
  • One would usually add DISTINCT to my query (SELECT DISTINCT CR FROM ...), so as to get every value just once. That would be a sort operation, but only on the few records already found. Again, no big deal.
  • If the string lengths have to be dealt with quite often, one might think of creating a computed column (calculated field) for it. This is available as of Ms Access 2010. But reading up on this shows that you cannot index calculated fields in MS Access. As long as this holds true, there is hardly any benefit from them. Applying LEN on the strings is usually not what makes such queries slow.
like image 25
Thorsten Kettner Avatar answered Oct 18 '22 15:10

Thorsten Kettner