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

后端 未结 9 1522
野趣味
野趣味 2020-12-16 14:19

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

相关标签:
9条回答
  • 2020-12-16 14:21

    For use the max in column definition, i suggest the right approach:

    create or replace FUNCTION F_GET_MAX_LENGTH_TAB_COLUMN
    (
      pCOLUMN_NAME  IN VARCHAR2 
    , pTABLE_NAME   IN VARCHAR2 
    , pOWNER        IN VARCHAR2 
    ) RETURN NUMBER AS 
      vLength NUMBER;
    BEGIN
    
      BEGIN
        SELECT  DATA_LENGTH 
        INTO    vLength
        FROM ALL_TAB_COLUMNS 
        WHERE 
            COLUMN_NAME = pCOLUMN_NAME 
        AND TABLE_NAME  = pTABLE_NAME 
        AND OWNER       = pOWNER
        ;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          vLength := 0;
      END;
    
      RETURN vLength;
    END F_GET_MAX_LENGTH_TAB_COLUMN;
    

    Just call the function:

    SELECT F_GET_MAX_LENGTH_TAB_COLUMN(
        pCOLUMN_NAME => 'AGN_ST_NOME', 
        pTABLE_NAME  => 'GLO_AGENTES', 
        pOWNER => 'MGGLO' ) 
    FROM DUAL;
    
    0 讨论(0)
  • 2020-12-16 14:30

    w/o function:

    select 
      rpad(tbl.column_name, length_info.max_length+1, ' ') as target_string
    from 
      table_name tbl,
      (
        select max(length(column_name)) max_length 
        from my_table
      ) 
        length_info
    

    with your function:

    select 
      rpad(tbl.column_name, MaxLengthFunc + 1, ' ') as target_string
    from 
      my_table tbl
    

    declare your function as determinictic for better performance:

    create or replace function MaxLengthFunc 
    return number  
    deterministic
    as
      vMaxLen number;
    begin
    
      select max(length(column_name)) 
      into vMaxLen
      from table_name;
    
      return vMaxLen;
    
    end;
    
    0 讨论(0)
  • 2020-12-16 14:33

    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.

    0 讨论(0)
  • 2020-12-16 14:37
    select max(length(MyColumn)) as MaxLength
    from MyTable
    
    0 讨论(0)
  • 2020-12-16 14:37

    select max(LENGTH(column_name)) from table_name.

    0 讨论(0)
  • 2020-12-16 14:38
    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
    
    0 讨论(0)
提交回复
热议问题