Solved

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

Posted on 2009-05-18
7
189 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now