SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 find the longest text in sql
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Madde
Starting Member

1 Posts

Posted - 01/22/2013 :  16:08:30  Show Profile  Reply with Quote
hi everyone :]

i'm trying to find the longest text in my column row DNAME. i have succeded in doing so but i cant seem to show the longest text only the ammout of length:

SELECT MAX(CHAR_LENGTH(DNAME)) as 'Max length'
FROM DEPT;


this will give the answer

+------------+
| max length |
+------------+
| 10 |
+------------+


what i want to show is

+--------------+--------------+
| DNAME | Max LENGTH |
+--------------+--------------+
| ACCOUNTING | 10 |
+--------------+--------------
i tried with:

SELECT DNAME, MAX(CHAR_LENGTH(DNAME)) as 'Max length'
FROM DEPT HAVING MAX(CHAR_LENGTH(DNAME));


(this will only give the first DNAME in the column row, not the one with the longest length and the max length totally in the column row)

and

SELECT DNAME as 'Max length'
FROM DEPT HAVING MAX(CHAR_LENGTH(DNAME));

(this wil only give the first DNAME in the column row and not the one with the longest length)

I'm using putty :]
would appreciate any answer :]

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 01/22/2013 :  16:52:25  Show Profile  Reply with Quote
This isn't perfect (coudl return more than one row), but might get you going:
SELECT 
	DNAME as 'Max length'
FROM 
	DEPT 
WHERE
	CHAR_LENGTH(DNAME) = 
	(
		SELECT MAX(CHAR_LENGTH(DNAME))
		FROM DEPT
	)
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 01/23/2013 :  00:16:44  Show Profile  Reply with Quote

SELECT 
	DNAME,
        CHAR_LENGTH(DNAME) as 'Max length'
FROM 
	DEPT 
WHERE
	CHAR_LENGTH(DNAME) = 
	(
		SELECT MAX(CHAR_LENGTH(DNAME))
		FROM DEPT
	)


This is SQL Server forum.. You may get quick response by posting MySql relarted posts in www.dbforums.com


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000