comma separated value in sql [duplicate] - sql

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.

Related

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

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) || '"'

Selecting rows with specific values in a column [duplicate]

This question already has answers here:
Select from any of multiple values from a Postgres field
(3 answers)
Closed 5 years ago.
I'm new to SQL. I've got my table with 3 columns. Given a list of values (lets say ints for example 1, 4, 3) I want to select rows in which the second columns value equals something that the given list contains.
Is it possible to pass a list as an argument in SQL?
select * from TABLE1 where ...
How do i finish this statement for it to work as i explained?
I'm using PostgreSQL 9.4
Try the following query:
SELECT *
FROM TABLE1
WHERE col2 IN (1, 4, 3);
Most flavors of SQL support the IN clause, which allows comparing a column against a sequence of values. In the query above, we compare col2 against a collection of numbers, but we can equally compare against string literals, etc.

Using COALESCE directly within a SELECT to return comma separated values from another table [duplicate]

This question already has answers here:
How can I pull a list of ID's from a SQL table as a comma-separated values string?
(7 answers)
Closed 7 years ago.
I am trying to retrieve a list of comma separated product categories for a product.
Here is an idea of what I am trying to do:
SELECT *, (
SELECT CategoryName
FROM ProductCategories pc
WHERE p.ProductId=pc.ProductId) as ProductCategories
FROM Products p
I need the product categories to be in a comma separated list. Is there a way to do it using COALESCE?
No, you can't use COALESCE to do that. COALESCE just returns the first non null argument that you pass it.
You want How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?

how to concatenate more than two columns in plsql developer? [duplicate]

This question already has answers here:
Oracle SQL, concatenate multiple columns + add text
(6 answers)
Closed 7 years ago.
when I run the below query
select concat(column1,column2,column3) as concatcolumn from table
I get an error "ORA-00909:INVALID NUMBER OF ARGUMENTS"
Concat only takes two arguments.
See: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions026.htm
Use the concatenation operator:
select column1 || column2 || column3 ...
select ([column1]+','+[column2]+','+[column3]) as concatcolumn from table
Try above the query.it may changes on different type of column data type.

Query to get column value comma separated [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Is there an Oracle SQL query that aggregates multiple rows into one row?
Agregate rows in Oracle SQL statement
I am working with Oracle 10g. I want to have a comma separated String from a column in a table.
e.g.
Table : Customer
Columns: id and name
Data:
id-------name
1-------John
2-------Galt
3-------Howard
4-------Roark
Output of query should be Jon,Galt,Howard,Roark
Ok, got it, all I wanted was this:
SELECT WM_CONCAT(NAME) FROM CUSTOMER;
Marking all comments as +1. Thanks guys.