Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-05-18
7
193 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL NULL vs Blank 26 39
Run SQL Server Proc from Access 11 37
average of calculation (TSQL) 4 26
Using SQL*PLUS issue with where statement with trunc function 3 27
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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

790 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