473,473 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return comma seperated value

1 New Member
Hi
I want to return a set of value with comma seperated in a single row with out using cursorts.

Eg.

Col 1
A
A
B

I want the result to be A,B.

Is there any SQL Server built in function does this?
Appreciate your help.

Thanks
Sep 4 '07 #1
2 2214
Mam139
2 New Member
Hi
I want to return a set of value with comma seperated in a single row with out using cursorts.

Eg.

Col 1
A
A
B

I want the result to be A,B.

Is there any SQL Server built in function does this?
Appreciate your help.

Thanks


Yes, there is a function called coalesce which works fro your problem.

Try this.

Declare @Var Varchar(10)
Select @Var = COALESCE(@Var+',' , ' ') + Col1 FROM Tab_Name
SELECT @Var


Have a nice day
Sep 5 '07 #2
Jim Doherty
897 Recognized Expert Contributor
Hi
I want to return a set of value with comma seperated in a single row with out using cursorts.

Eg.

Col 1
A
A
B

I want the result to be A,B.

Is there any SQL Server built in function does this?
Appreciate your help.

Thanks
Create the below function in the pubs database so that you can see its results first and then you can amend it to suit your requirement....

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.UDF_ConcatToLine  (@proxy char(1))
  2. RETURNS varchar(5000)
  3. AS
  4. BEGIN
  5. DECLARE @Concat varchar(5000), @Delimiter char
  6. SET @Delimiter = ','
  7. SELECT @Concat = COALESCE(@Concat + @Delimiter, '') + LTRIM(STR(royaltyper))
  8.      FROM  (SELECT DISTINCT royaltyper 
  9.             FROM dbo.titleauthor) derived
  10.   RETURN ( SELECT @Concat AS [My_Single_Line])
  11. END

Use it in an SQL statement like this

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT dbo.UDF_ConcatToLine('') AS [My Single Line]
  2. FROM         dbo.titleauthor

Replace the bold elements outlined above with your field name and table name

Its somewhat specific but you could amend it eventually to feed in the fieldname name and tablename to make it generic

Regards

Jim
Sep 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to...
11
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4
3
by: Gary Smith | last post by:
Hi, I've got a field that contains a list of rooms. In most cases, this contains a single ID. However, under some circumstances, the field may contain a list of two IDs which are broken by a...
3
by: Maqsood Ahmed | last post by:
Hello, Here is the scenario: I have a datagrid on a winform and a datatable is set as its datasource. I have added DataGridTableStyle in datagrid's TableStyles property. Some of Gridstyles are...
7
by: Sick | last post by:
My application populates a ListBox from a .TXT file. In the textfile there are prices with both dots as well as comma's for decimal indication. Example: 1234990; xg-tr-45; 1700,50; 0 2662666;...
3
by: dfetrow410 | last post by:
I need make a comma seperated list, but whwn I build the list I get a comma at the end. How do I remove it? foreach (ListItem lst in REIPropertyType.Items) { if (lst.Selected == true) {...
2
nehashri
by: nehashri | last post by:
i hv a database in access wid Asp as front end. ven given a word in search command(of the front end) it shud go to a table in which each field has words serated by comma. each word shud b checked...
0
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I know i can use cpytostmf/cpytoimpf to create the...
1
by: (2b|!2b)==? | last post by:
I have the following line in my code, which is supposed to skip commas and white space and to read a comma seperated value string into the appropriate variables: sscanf(temp.c_str(),...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.