;

How to Find the Shortest and the Longest String from a Column of a table in SQL Server


Tutorialsrack 09/12/2020 SQL SQL Server

In this article, you will learn how to find the shortest string and the longest string from a column of a table in an SQL server. There are various ways to find the shortest and longest string from a column of the table in SQL Server.

Here is an example to find the shortest string and the longest string from a column of a table. Let's take this table for our example and find the shortest firstname and the longest firstname with their respective length from the table.

Table: tbl_emp

id

firstname

lastname

gender

city

1

Tom

Hardy

m

New York

2

Hugh

Jackman

m

Los Angeles

3

Scarlett

johansson

f

Manhattan

4

Chris

Hemsworth

m

Sydney

6

Harry

Potter

m

New York

7

Tom

Holland

m

New York

8

Sofia

vergara

f

Colombia

Example 1: Shortest and Longest String from a column of a table

Example 1: Shortest and Longest String from a column of a table
-- Shortest First Name
select top 1 firstname,len(firstname) as ShortestLength from tbl_emp order by len(firstname) asc, firstname asc

-- Longest First Name
select top 1 firstname,len(firstname) as LongestLength from tbl_emp order by len(firstname) desc, firstname asc
SQL
Output

firstname                           ShortestLength

----------------------------------- ----------------------------

Tom                                  3

 

(1 row(s) affected)

 

firstname                            LongestLength

-----------------------------------  -------------

Scarlett                               8

 

(1 row(s) affected)

Example 2: UsingCTE, DenseRank() and LEN() Function

Example 2: UsingCTE, DenseRank() and LEN() Function
with cte as
(
select *, LEN(firstname) as StringLength, DENSE_RANK() over (partition by len(firstname) order by firstname) as dRank from tbl_emp
)

select firstname,StringLength from cte where dRank = 1 and StringLength = (select MIN(StringLength) from cte)
UNION
select firstname,StringLength from cte where dRank = 1 and StringLength = (select max(StringLength) from cte)
SQL
Output

firstname                           StringLength

----------------------------------- ------------

Scarlett                              8

Tom                                   3

 

(2 row(s) affected)

I hope this article will help you to understand how to find the shortest string and the longest string from a column of a table in an SQL server.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags
string vs StringBuilder string vs String convert datetime nth highest salary 2nd or 3rd highest salary palindrome C# Programs Extension method Jquery Javascript PHP SQL Server MySQL SQL functions SQL Server Functions MySQL Functions Datetime functions PHP Programs email validation Email validation using jquery and javascript LINQ Programs SQL ALTER Statement SQL DROP Statement SQL SELECT Statement MySQL SHOW Statement C# DateTime C Program Pattern Program Generate random number and strings in C# JSON String in c# DataTable in c# Jquery Attributes Data Conversion Javascript Dates Javascript Date Functions MySQL DateTime Functions C# String String SQL DateTime Functions SQL Server DateTime Functions javascript Functions Jquery Functions Python Programming Python String Python Programs Python Functions Python String Functions String Functions Regular Expression in Javascript Regular Expression in JQuery Regular Expression Validation using Javascript Validation using Jquery Data Validation Resize an Image Bitmap image Graphics Image in C# QueryString in C# QueryString Python Datetime Functions Python Date Formating Date Formatting Globalization CultureInfo System.Globalization Python Programming Examples Python Pattern Programs Python List Python Dictionary C# DataType C# Function C# String Functions Javascript String Functions Javascript Array Functions Custom Date And Time Formatting Standard Date and Time Formatting C# IP Address Python IP Address Computer Networks Computer Networks-IP Addressing Session in C# Session in ASP.NET Session in ASP.NET MVC C# Session Session State WEB API Web API Error JSON Serialization ASP.NET WEB API Error Department Wise Nth Highest Salary Difference Between Jquery Events Javascript Events HTML & CSS CSS Properties Web Design Web Development CSS Styles Regular Expression in C# ASP.NET MVC Routing Custom Routing in ASP.NET MVC Routing in ASP.NET ASP.NET Web.Config ASP.NET MVC Web.Config Web.Config Configuration Java Programs Java Basic Programs Java Programming Examples Java DateTime Java String Control Statements in Java Software Development Tools Development Tools Javascript Object Javascript Operator JavaScript Object Properties JavaScript Object Methods Javascript Errors JQuery Errors Javascript Array Array methods Javascript Array Methods C# Keywords Windows Commands NodeJs Commands Linux Commands macOS Terminal Commands Base64 Encoding and Decoding SQL Server Error SQL Server Index MongoDB DateTime MongoDB Operators MongoDB Aggregation Operators SQL Server Constraints ReactJs Errors ReactJS Error Fixes ReactJs Modules SQL Server Backup SQL Server Variables ASP.NET Core ASP.NET Core Middleware ASP.NET Core Filters