Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column value length, not column max length of value

How do I get actual length of value in column?

My (oracle) sql command returns maximum length of value that can be inserted in the column instead of real length of value.

How to fix this?

SQL> SELECT typ, length(t1.typ)
     FROM AUTA_VIEW t1;

TYP        LENGTH(T1.TYP)
---------- --------------
BMW                    10
BMW                    10
BMW                    10
Ferrari                10
BMW                    10
Audi                   10
Audi                   10
Audi                   10
Ferrari                10
Ferrari                10
Mercedes               10
like image 640
Buksy Avatar asked Oct 25 '12 08:10

Buksy


People also ask

How do you find the maximum length of a column?

=MAX(LEN(C1:C2886)) Replace the C1:C2886 with your column and number of rows, then press Ctrl+Shift+Enter. It's very important you don't just press Enter, it won't work. The cell will now show the maximum length of the largest value for the column!

How do I get the maximum length of a column 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.

How do I find the length of a column of data in SQL?

Well, you can use the LEN() function to find the length of a String value in SQL Server, for example, LEN(emp_name) will give you the length of values stored in the column emp_name.

How can we get the length of each entry of a column Column_name of a DataFrame?

Get the number of columns: len(df. columns) The number of columns of pandas. DataFrame can be obtained by applying len() to the columns attribute.


1 Answers

LENGTH() does return the string length (just verified). I suppose that your data is padded with blanks - try

SELECT typ, LENGTH(TRIM(t1.typ))
FROM AUTA_VIEW t1;

instead.

As OraNob mentioned, another cause could be that CHAR is used in which case LENGTH() would also return the column width, not the string length. However, the TRIM() approach also works in this case.

like image 66
Andreas Fester Avatar answered Oct 30 '22 07:10

Andreas Fester