Oracle:如何指示截断的列结果?

时间:2015-08-13 18:39:14

标签: sql oracle

如何通过附加" ..."?

来截断查询结果并指明它已被截断

此示例截断超过2个字符的结果,但不幸的是要将 N when子句截断为 N 字符。

select 
  case length(x)
    when 1 then x
    when 2 then x
    else substr(x,1,2)||'...'
  end x
from (select 'ab' x from dual union select 'abc' from dual);

X
------
ab
ab...

有没有办法在查询中添加length(x) > 25等条件逻辑?

1 个答案:

答案 0 :(得分:4)

也许你只想要这个?

select (case when length(x) <= 2 then x
            else substr(x, 1, 2) || '...'
        end) as x

。 。

也就是说,使用case的条件形式,而不是常量形式。

以下是问题中表达的完整代码。

select 
  case when length(x) <= 2 then x
  else substr(x, 1, 2) || '...'
  end x
from (select 'ab' x from dual union select 'abc' from dual);
X
------
ab
ab...