oracle中求某列中最长行的长度

发布于 2024-09-12 07:20:17 字数 558 浏览 5 评论 0原文

有谁知道是否有办法找到最长的长度 Oracle 中的列中的行?

基本上我需要获取最长行的长度,然后使用该长度加 1 使用 SUBSTR 使列的输出比最长的字符串长一个字符。


但是, MAX(LENGTH(column_name)) AS MAXLENGTH 方法为我提供了我想要的数字,但是当我尝试将其与 SUBSTR(column_name,1, MAXLENGTH) 一起使用时,我收到无效标识符错误。

所以我做了一个函数来返回我想要的数字然后使用:

SUBSTR(column_name,1,maxlengthfunc)

这给了我以下输出:

SUBSTR(NAME,1,MAXLENGTHFUNC)

而不是:

SUBSTR(NAME, 1, 19)

并且它没有像我需要的那样缩小输出列的大小。

运行时也

RTRIM(name)||' '

不起作用。

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.


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 work when run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一杯敬自由 2024-09-19 07:20:17

这适用于 VARCHAR2 列。

select max(length(your_col))
from your_table
/

CHAR 列显然都是相同的长度。如果列是 CLOB,您将需要使用 DBMS_LOB.GETLENGTH()。如果它很长,那就真的很棘手了。

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.

刘备忘录 2024-09-19 07:20:17
SELECT max(length(col_name)+1) as MyOutput
FROM table_Name

正常输出看起来像

   MyOutput
1     5

新输出看起来像

   MyOutput
1     6
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
可爱咩 2024-09-19 07:20:17
select max(length(MyColumn)) as MaxLength
from MyTable
select max(length(MyColumn)) as MaxLength
from MyTable
停顿的约定 2024-09-19 07:20:17

这应该可以满足您的要求:

select max(length(MyColumn)) from MyTable;

根据您想要实现的目标,您可能还想知道您可以输出列中的数据加上一个空格,如下所示:

select rtrim(MyColumn)||' ' from MyTable;

This should do what you want:

select max(length(MyColumn)) from MyTable;

Depending on what you are trying to achieve, you may also be insterested to know that you can output the data in the column plus exactly one space like this:

select rtrim(MyColumn)||' ' from MyTable;
嘦怹 2024-09-19 07:20:17

从表名中选择 max(LENGTH(列名))。

select max(LENGTH(column_name)) from table_name.

匿名。 2024-09-19 07:20:17

不带函数:

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

使用您的函数:

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

将您的函数声明为确定性函数以获得更好的性能:

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;

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;
关于从前 2024-09-19 07:20:17

为了使 maxlength 可用,您可能需要从嵌入的选择中获取它

select <do something with maxlength here> 
from
(select x.*, 
( select max(length(yourcolumn)) from yourtable) as maxlength 
from yourtable x)

To make the maxlength useable you may want to get it from a imbedded select

select <do something with maxlength here> 
from
(select x.*, 
( select max(length(yourcolumn)) from yourtable) as maxlength 
from yourtable x)
别闹i 2024-09-19 07:20:17

要将列中的所有值填充到最长值 +1,您可以执行以下操作:

SELECT RPAD( column_name ,(SELECT MAX(LENGTH( column_name ))+1 FROM table)) FROM table;

To pad all values in a column to the longest value +1 you can do:

SELECT RPAD( column_name ,(SELECT MAX(LENGTH( column_name ))+1 FROM table)) FROM table;
风透绣罗衣 2024-09-19 07:20:17

为了在列定义中使用最大值,我建议正确的方法:

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;

只需调用该函数:

SELECT F_GET_MAX_LENGTH_TAB_COLUMN(
    pCOLUMN_NAME => 'AGN_ST_NOME', 
    pTABLE_NAME  => 'GLO_AGENTES', 
    pOWNER => 'MGGLO' ) 
FROM DUAL;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文