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;#
SELECT TOP 1 * FROM friends WHERE len(firstName) = (SELECT min(len(firstName)) FROM friends);
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.
=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!
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.
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.
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.LEN
on the strings is usually not what makes such queries slow.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