Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the length of the longest row in a column in oracle

Tags:

Does anybody know if there is a way to find what the length of the longest row in a column in Oracle?

Basically I need to get the length of the longest row and then use that length plus 1 with SUBSTR to make the output of the column one character longer than the longest string.

Thanks

EDIT:

Thanks for the advice.

However, the MAX(LENGTH(column_name)) AS MAXLENGTH approach gives me the number I want but when I try to use it with SUBSTR(column_name,1, MAXLENGTH) I get an invalid identifier error.

SO I made a function to return the numberI wanted then used:

SUBSTR(column_name,1,maxlengthfunc) 

This gave me the following output:

SUBSTR(NAME,1,MAXLENGTHFUNC) 

Rather than:

SUBSTR(NAME, 1, 19) 

And it didn't shrink the output column size like I needed.

Also

RTRIM(name)||' ' 

didn't do anything for me in SQL developer.

Thanks.

like image 833
electricsheep Avatar asked Jul 29 '10 11:07

electricsheep


People also ask

How do you find Max string length in SQL?

Use the built-in functions for length and max on the description column: SELECT MAX(LEN(DESC)) FROM table_name; Note that if your table is very large, there can be performance issues.

What is the maximum length of column in Oracle?

You're absolutely right: Table names are 30 characters maximum same as column names. No, don't believe so - but Oracle does. 30 characters max, from what I recall.

How do I find the longest name in SQL?

SQL Server LEN() Function The LEN() function returns the length of a string. Note: Trailing spaces at the end of the string is not included when calculating the length.

What is long ops in Oracle?

This view displays the status of various operations that run for longer than 6 seconds (in absolute time). These operations currently include many backup and recovery functions, statistics gathering, and query execution, and more operations are added for every Oracle release.


2 Answers

This will work with VARCHAR2 columns.

select max(length(your_col)) from your_table / 

CHAR columns are obviously all the same length. If the column is a CLOB you will need to use DBMS_LOB.GETLENGTH(). If it's a LONG it's really tricky.

like image 127
APC Avatar answered Sep 19 '22 18:09

APC


SELECT max(length(col_name)+1) as MyOutput FROM table_Name 

Normal output would look like

   MyOutput 1     5 

New output would look like

   MyOutput 1     6 
like image 30
sealz Avatar answered Sep 22 '22 18:09

sealz