I need to create a view with an aggregate column of nvarchar2(4000)
in Oracle 11g.
What I have:
Table1
with every column of type nvarchar2(4000)
:
Col_Name txt_value
X a
X b
X c
Y a
Y c
The result should be a view like this with both columns of type nvarchar2(4000)
:
Col_Name txt_value
X a;b;c
Y a;c
I have tried with:
CREATE VIEW dict (col_Name, txt_value) AS
SELECT col_Name, LISTAGG (txt_value,';') WITHIN GROUP (ORDER BY txt_value)
FROM table1
GROUP BY col_Name
The only problem is that LISTAGG()
is converting the result in a column of type varchar2(4000)
bit I need nvarchar2
.
Best Answer
As the documentation describes:
So unless you pass the column as
RAW
, the result will beVARCHAR2
, and that is not good enough, because that means data loss during conversion.LISTAGG
works withVARCHAR2
orRAW
, so to simply put it, you need to convert everything toRAW
, then convert the result back toNVARCHAR2
.Below is an example:
My default characterset does not support the
€
sign.As you can see, i get a
?
instead of it usingVARCHAR2
.If I simply try to use
LISTAGG
on theNVARCHAR2
column, data will be lost during conversion:If I try to cast the result, still not good enough, as data was lost during the conversion to
VARCHAR2
:The remaining option is, to convert to
RAW
:Converting a
VARCHAR2
delimiter will mess up the data, I need to specify the delimiter also inNVARCHAR2
, so instead of','
, I have to useN','
: