Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to select the rows which has the maximum characters in the column

Posted on 2009-05-18
7
Medium Priority
?
199 Views
Last Modified: 2012-05-07
Sample Data:
ID      name
2      adadas
2      adadasdad
2      adadasdasdasd
3      sdada
3      sadasdasd
3      ggggggggggggggg
4      xxxx
4      fgfgfgfg
4      fdfd
4      ghhjhjhjhjhjj
5      fhgfhf
6      opopoppp
6      opo

I wanted to select the ID and the name which has the maximum characters, can someone please help me find.
0
Comment
Question by:asadeen
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24415901
select top1 ID from urtable order by len(name) desc
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24415905
select top (1) * from SomeTable order by len([Name]) desc
0
 

Author Comment

by:asadeen
ID: 24415958
this will return one record from the table which contains the maximum, but I wanted the result to be
2      adadasdasdasd
3      ggggggggggggggg
4      ghhjhjhjhjhjj
5      fhgfhf
6      opopoppp
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:asadeen
ID: 24416040
I found the answer
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24416058
U should have given the sample output , :)
0
 

Accepted Solution

by:
asadeen earned 0 total points
ID: 24416067
SELECT ID, Name from mytable as mt1 WHERE LEN(Name) = (SELECT MAX(LEN(Name)) from mytable as mt2 WHERE mt2.ID = mt1.ID)
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24416079
I would do it this way instead.

SELECT ID, Name from mytable as mt1
WHERE [Name] = (SELECT top (1) [Name] from mytable as mt2 WHERE mt2.ID = mt1.ID order by len([name]))
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question