---
title: "How to Write a Query to Find the Longest String in a Column in SQL Server?"  
description: "How to Write a Query to Find the Longest String in a Column in SQL Server?"  
author: "Ravi Vishwakarma"  
published: 2024-07-16  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160928/how-to-write-a-query-to-find-the-longest-string-in-a-column-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# How to Write a Query to Find the Longest String in a Column in SQL Server?

I need a [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to find the longest [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in the `Description` [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) of the `Products` table. How can I achieve this?

## Replies

### Reply by Ravi Vishwakarma

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

```plaintext
-- Technique 1
SELECT TOP 1 [Description]
FROM
    Article
ORDER BY
    LEN([Description]) DESC;
```

## Example 2

```plaintext
-- Technique 2
SELECT TOP 1 [Description], LEN([Description]) AS length_of_string
FROM
    Article
ORDER BY
    length_of_string DESC;
```

## Example 3

```plaintext
-- Technique 3
select top 1 *
from (
	select [Description], LEN([Description]) AS [S_Length] from Article
) AS Article
order by [S_Length] desc
```

## Example 4

```plaintext
-- 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
```

## Output

![How to Write a Query to Find the Longest String in a Column in SQL Server?](https://www.mindstick.com/mindstickforums/7080febe-caf6-44a4-91c8-45d74ced80db/images/91358134-0654-436b-9bcb-b0ae79d2cadf.png)

## Read more

[**Explain the SQL Server backups and their types**](https://www.mindstick.com/articles/336326/explain-the-sql-server-backups-and-their-types)

[**Define the PIVOT Table with examples in the SQL server.**](https://www.mindstick.com/articles/336334/define-the-pivot-table-with-examples-in-sql-server)

[**MERGE statement in SQL Server to perform upserts**](https://www.mindstick.com/blog/304491/merge-statement-in-sql-server-to-perform-upserts-update-inserts)

[**Explain the SQL CURSOR with an example.**](https://www.mindstick.com/blog/304490/explain-the-sql-cursor-with-example)


---

Original Source: https://www.mindstick.com/forum/160928/how-to-write-a-query-to-find-the-longest-string-in-a-column-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
