I have a table structure similar to one given below
1001 Jon
1002 Jacob
1003 Cayle
I want to fetch records from table in a way that it should look like
1001 – Jon, 1002 – Jacob, 1003 – Cayle
Can anybody suggest some easy way.
I have a table structure similar to one given below
1001 Jon
1002 Jacob
1003 Cayle
I want to fetch records from table in a way that it should look like
1001 – Jon, 1002 – Jacob, 1003 – Cayle
Can anybody suggest some easy way.
The easiest way on Oracle to achieve that is listagg
. Try something like this:
create table parameter as
select 1001 as id, cast('Jon' as varchar2(10)) as name from dual union all
select 1002, 'Jacob' from dual union all
select 1003, 'Cayle' from dual
;
select listagg(id||' - '||name, ', ') within group (order by id) as list
from parameter
;
LIST
------------------------------------------
1001 - Jon, 1002 - Jacob, 1003 - Cayle
Yet another solution on Oracle. I think it will be easy to port to some other RDBMS:
select
rtrim(
xmlagg(
хmlElement(tag, id||' - '||name, ', ').еxtract('//text()') order by id
).getStringVal(), ', ') as list
from parameter
;
LIST
-------------------------------------------
1001 - Jon, 1002 - Jacob, 1003 - Cayle
If your table is really very small, than you may define your specific function. This function can be easily ported to any RDBMS:
create or replace function paramList return varchar2 is
ret varchar2(1024) := null;
begin
for row_ in (select id||' - '||name as tag from parameter) loop
ret := ret||', '||row_.tag;
end loop;
return ltrim(ret, ', ');
end paramlist;
/
show errors
col parameters format a40
select paramList parameters from dual;
PARAMETERS
----------------------------------------
1001 - Jon, 1002 - Jacob, 1003 - Cayle
Or more generic function:
create or replace function concatRows (cursor_ sys_refcursor) return varchar2 is
ret varchar2(32767);
tmp varchar2(32767);
begin
loop fetch cursor_ into tmp;
exit when cursor_%notfound;
ret := ret || ', ' || tmp;
end loop;
return ltrim(ret, ', ');
end ;
/
select concatRows(cursor(
select id||' - '||name from parameter
)) parameters from dual
;
PARAMETERS
----------------------------------------
1001 - Jon, 1002 - Jacob, 1003 - Cayle
One more generic function with collect
:
create or replace function arrayagg (arr sys.ODCIVarchar2List, delimiter varchar2 default ', ') return varchar2 is
ret varchar2(32767);
begin
for i in arr.first.. arr.last loop
ret := ret||arr(i)||delimiter;
end loop;
return rtrim(ret, delimiter);
end arrayagg;
/
select arrayagg(cast(collect(id||' - '||name) as sys.ODCIVarchar2List)) parameters
from parameter
;
PARAMETERS
-----------------------------------------
1001 - Jon, 1002 - Jacob, 1003 - Cayle
In SQL Server you can get it using STUFF and FOR XML
select STUFF((
SELECT ', ' + CONCAT(CAST(id as varchar(10)), ' - ', name)
FROM mytable md
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
;
GO
| (No column name) |
| :-------------------------------------- |
| 1001 - Jon, 1002 - Jacob, 1003 - Cayle |
dbfiddle here
SELECT listagg(id||' ' || '-'||' ' || name,', ') within GROUP (ORDER BY id) con FROM table;