LISTAGG内的RTRIM?

时间:2019-07-19 17:01:58

标签: sql oracle

美好的一天。我正在编写一个使用LISTAGG并返回结果的查询。这是我到目前为止的代码。

   select 
    listagg(rtrim(shop_cde, 1), ', ') within group (order by shop_cde) col1,
    business_cde
    from mytable
    group by business_cde

我希望这会返回结果,对其进行汇总,并在shop_cde的右边减去1个字符。但是,似乎没有修剪发生。 Shop_cde仍完整显示。有人知道如何在LISTAGG函数中进行TRIM吗?

3 个答案:

答案 0 :(得分:3)

trim()函数通常用于删除开头和结尾的空格(尽管您也可以删除其他字符)。

如果要舍弃最后一个字符,请使用substr()

select listagg(substr(shop_cde, 1, length(shop_cde) - 1), ', ') within group (order by shop_cde) col1,
       business_cde
from mytable
group by business_cde

答案 1 :(得分:2)

如果要从右侧删除字符的给定数字,请使用substr;如果要给定字符的给定数字,请使用rtrim。 / em>已消除。分别删除左侧的substr(..., 2)ltrim

   select 
    listagg(substr(shop_cde, -1), ', ') within group (order by shop_cde) col1,
    business_cde
    from mytable
    group by business_cde

答案 2 :(得分:0)

您应该使用rtrim(listagg(....))

select 
rtrim(listagg(shop_cde, ', ') within group (order by shop_cde) ) col1,
business_cde
from mytable
group by business_cde