Oracle DB, concat all values from 1 column using "," separator [duplicate] - sql

This question already has answers here:
SQL Query to concatenate column values from multiple rows in Oracle
(10 answers)
Closed 3 years ago.
I have Oracle DB table with 1 column UUID
How can I concatenate all the UUID values in one string separated by "," so the output will be like
"id1","id2","id3","id4"....

You can use listagg():
select listagg(uuid, ',') within group(order by uuid) uuids
from mytable
This will give you a single record with a single column containing all uuids, separated with a comma.
Note that listagg() returns a varchar, hence the output cannot be more than 4000 bytes.
Also note that:
the comma is the default separator, so in your case it is not strictly necessary to pass a second argument to the function
I added an order by clause in order to generate a deterministic result: this option is not mandatory, so if the ordering of items in the list does not matter to you then you can leave it apart (in which case you will get the uuids in an undefined order)

You can use listagg() -- assuming there are not too many values. The maximum string size is 4000 bytes.
selest '"' || listagg(uuid, '","') within group (order by uuid) || '"'

Related

How I can use 'listagg' with clob [duplicate]

This question already has answers here:
LISTAGG function: "result of string concatenation is too long"
(14 answers)
Oracle - convert column values to comma separated values as CLOB without using XMLAGG
(1 answer)
Closed 4 months ago.
I need to concatenate several clubs into one string with a delimiter between them and put it into the club. But I have received different errors with the same meaning - The combined string is so large.
I have next sql:
select listagg(clob_column, '***') as text, doc_id
from table
group by doc_id;
Where clob_column may be really long string.
How can I concatenate cells from one column with a delimiter in 19c Oracle DB?

How to count the number of commas (',') in a SQL table column?

I have a table in Teradata. This table has a column named text_column that contains some text in format varchar(25).
I need to count the number of ',' in this column.
One method uses the difference of lengths of strings:
select ( character_length(text_column) - character_length(oreplace(text_column, ',', '')) as num_columns

Combining two columns of a table and returning it as a single column separated by a character

I'm using oracle sql developer for the first time. I want to create a view with two columns of a table combined and seperated by a slash.
I have done this in ssms but when I write the same code for sql dev it returns me problems like 'the specified number is invalid.
SELECT ID AS W_ID, CAST(data1 AS VARCHAR)+' - '+data2 AS W_CODE, +data3 AS W_TEXT
FROM table1
WHERE data3=1;
how can I translate this query in Oracle ?
Oracle uses the standard || operator for string concatenation, not +:
SELECT ID AS W_ID,
(data1 || ' - ' || data2) AS W_CODE,
data3 AS W_TEXT
FROM table1
WHERE data3 = 1;
Casting to a string is unnecessary. Oracle will do that automatically. If you do, TO_CHAR() is the more colloquial method in Oracle, because it lets you control the format for numbers and dates.
I don't understand the purpose of +, so I removed it. Perhaps you want abs()?

comma separated value in sql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Concat all column values in sql
I have a table
ID | Name
1 | X
2 | y
3 | z
I have to show values in column Name as comma separated i.e x,y,z.
One way I can do is looping the values of column "Name" and displaying as comma separated.
Is there is a other way to do it.Please help.
Even though this is a duplicate of several other questions, I'd like to give an answer, because the easiest way to do this has changed recently. Oracle has provided the very nifty LISTAGG function with the following syntax:
SELECT
LISTAGG(name, ',') WITHIN GROUP (ORDER BY name)
FROM
my_table;
LISTAGG is available since Oracle 11.2.

Select number of comma delimited list items

I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?
alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)
select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;
for better utilize the index, you should seek to normalize your table
If you're using PostgreSQL, you could also use regular expressions.
However, a better question might be why you have a single column with comma-separated values instead of multiple columns.
If you count a string with 14 commas as having 13 commas, then this will work:
SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'
% matches any string (including zero length).
In PostgreSQL you can do:
select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;