oracle中求某列中最长行的长度
有谁知道是否有办法找到最长的长度 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这适用于 VARCHAR2 列。
CHAR 列显然都是相同的长度。如果列是 CLOB,您将需要使用 DBMS_LOB.GETLENGTH()。如果它很长,那就真的很棘手了。
This will work with VARCHAR2 columns.
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.
正常输出看起来像
新输出看起来像
Normal output would look like
New output would look like
这应该可以满足您的要求:
根据您想要实现的目标,您可能还想知道您可以输出列中的数据加上一个空格,如下所示:
This should do what you want:
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:
从表名中选择 max(LENGTH(列名))。
select max(LENGTH(column_name)) from table_name.
不带函数:
使用您的函数:
将您的函数声明为确定性函数以获得更好的性能:
w/o function:
with your function:
declare your function as
determinictic
for better performance:为了使 maxlength 可用,您可能需要从嵌入的选择中获取它
To make the maxlength useable you may want to get it from a imbedded select
要将列中的所有值填充到最长值 +1,您可以执行以下操作:
To pad all values in a column to the longest value +1 you can do:
为了在列定义中使用最大值,我建议正确的方法:
只需调用该函数:
For use the max in column definition, i suggest the right approach:
Just call the function: