To find the longest string in a column in SQL Server, you can use the LEN function to get the length of each string and then use
ORDER BY in combination with TOP 1 to retrieve the longest string.
Here's how you can write the query:
Example 1
-- Technique 1
SELECT TOP 1 [Description]
FROM
Article
ORDER BY
LEN([Description]) DESC;
Example 2
-- Technique 2
SELECT TOP 1 [Description], LEN([Description]) AS length_of_string
FROM
Article
ORDER BY
length_of_string DESC;
Example 3
-- Technique 3
select top 1 *
from (
select [Description], LEN([Description]) AS [S_Length] from Article
) AS Article
order by [S_Length] desc
Example 4
-- Technique 4
select top 1 *
from (
select
[Description],
LEN([Description]) AS [S_Length],
ROW_NUMBER() OVER( ORDER BY LEN([Description]) DESC) AS ROWNO
from Article
) AS Article
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To find the longest string in a column in SQL Server, you can use the
LENfunction to get the length of each string and then useORDER BYin combination withTOP 1to retrieve the longest string.Here's how you can write the query:
Example 1
Example 2
Example 3
Example 4
Output
Read more
Explain the SQL Server backups and their types
Define the PIVOT Table with examples in the SQL server.
MERGE statement in SQL Server to perform upserts
Explain the SQL CURSOR with an example.