473,667 Members | 2,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Server 2005: How to turn multiple rows into a single column

2 New Member
How do I display multiple rows in a single column within the SQL Server 2005?

Sample Data

-----------------------------------------------------
Cust Name Cust_Order
------------------------------------------------------
ABCD ------ Calculator
ABCD ------ Radio
ABCD ------ TV
EFGH ------ Radio
IJKL ------ TV

Sample Result

-----------------------------------------------------
Cust Name Cust_Order
------------------------------------------------------
ABCD ------ Calculator, Radio, TV
EFGH ------ Radio
IJKL ------ TV
Dec 13 '07 #1
2 2986
jamesd0142
469 Contributor
This can be modified to suit i think...

Expand|Select|Wrap|Line Numbers
  1. --results on 1 row........................
  2. DECLARE @a varchar(8000)
  3. SELECT @a=COALESCE(@a, '') + col01 + space(1) FROM (SELECT distinct (col01) from Dataset_Rows) as a
  4. SELECT rtrim(ltrim(@a)) AS [MyValues]
  5.  
Dec 13 '07 #2
Jim Doherty
897 Recognized Expert Contributor
This can be modified to suit i think...

Expand|Select|Wrap|Line Numbers
  1. --results on 1 row........................
  2. DECLARE @a varchar(8000)
  3. SELECT @a=COALESCE(@a, '') + col01 + space(1) FROM (SELECT distinct (col01) from Dataset_Rows) as a
  4. SELECT rtrim(ltrim(@a)) AS [MyValues]
  5.  

Hi James and eaocomp

The modification required to make this work for the poster centres around the fact that a concatenated list of values is required for each of the Customer names given. The code snippet you highlighted for amendment is not CustName centric (unless of course Dataset_Rows has some restrictive criteria based on the CustName) therefore it would return everything if you get me.

In short, for each customer the SQL is required to trundle down the products column as it were, and grab each unique mention of a product and fetch those values into 'one line' per customer separating out the products with a delimiter value which in this case is a required comma.

If the user creates a 'user defined function' to perform this functionality then this can be referenced in any views with the 'required' parameter being passed from the 'CustName' column value and the defined line of products return values then become visible in the view as a displayed column. We know that we can reference views in any stored procedures so we get the best of both worlds should they ever be required later.

This is the sample SQL that demonstrates the point of calling the UDF(User defined function) the udf function call being bolded

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT DISTINCT [Cust Name], dbo.UDF_CustOrder([Cust Name]) AS Cust_Order
  3. FROM dbo.[Sample Data]
  4.  

The user defined function below to make this work is based on the table name 'Sample Data' as given obviously amend that to suit (hate spaces in object names but there you go) as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE FUNCTION dbo.UDF_CustOrder
  3. ( @custname varchar (50) )
  4. RETURNS varchar(8000)
  5. AS
  6. BEGIN
  7. DECLARE @Custorder varchar(255), @delimiter char
  8. SET @delimiter = ','
  9. SELECT @custorder = COALESCE(@custorder + @delimiter, '') +space(1)+ Cust_Order FROM (SELECT DISTINCT [Cust Name],Cust_Order FROM [Sample Data] where [Cust Name]=@CustName) c
  10. RETURN ( SELECT ltrim(rtrim(@CustOrder)) AS [CustOrder])
  11. END
  12.  

Hope this helps

Jim :)
Dec 14 '07 #3

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

Similar topics

3
16329
by: JB | last post by:
To anyone that is able to help.... What I am trying to do is this. I have two tables (Orders, and OrderDetails), and my question is on the order details. I would like to set up a stored procedure that essentially inserts in the orders table the mail order, and then insert multiple orderdetails within the same transaction. I also need to do this via SQL 2000. Right now i have "x" amount of variables for all columns in my orders tables,...
16
43349
by: bika | last post by:
I have a table that has values as follows: PersonID Degree 55 MD 55 Phd 55 RN 60 MD 60 Phd I need a create a query that will give me output like this:
3
1520
by: Asaf | last post by:
Hi, I have a MSSQL 2005 test DB with two tables: Table "T1Customers": T1CustomersRowEnum (PK, int, Not Null) T1CustomersFullName (nvarchar(50) null) Table "T2Details":
7
43134
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is how the data is at the moment: Company_ID Material 0x00C00000000053B86 Lead 0x00C00000000053B86 Sulphur 0x00C00000000053B86 Concrete
3
28081
Atli
by: Atli | last post by:
Hi. I've been trying to insert multiple rows into a table using a single INSERT statement in MSSQL / SQL Server 2005. I could of course cheat and have my C# code insert each row using some sort of a loop, but where is the fun in that :-) If you'r familliar with MySQL, this MySQL query would do what I am trying to do with MSSQL / SQL Server 2005. INSERT INTO UserInfo(InfoName, InfoValue) VALUES
0
12885
Coldfire
by: Coldfire | last post by:
Since i cannot show the differences in a two-column like table. I am first putting MS SQL Server 2005 and then MySQL 5.x. MS SQL Server 2005 Brief Overview - SQL Server is a full-fledged database system developed specifically for large enterprise databases. All advanced features of a relational database are fully implemented. - Once you purchase the product, you are only limited to the Sybase-derived engine.
1
3210
by: gaholmes | last post by:
Hi, I am stumped and was hoping someone could help me out. Any help is appreciated. I have a view that looks sort of like this (but with a lot more entries of course) UniqueIdentifyier Column1 Column2 1 9999 100
6
6411
chiragvithlani
by: chiragvithlani | last post by:
In SQL Server 2005... with some 11K rows I am having two tables T1 and T2(as bkup of T1), now i need one column data of T2 as i accidently updated T1 column data, (Update T1.colx with T2.colx) I tried with <code> update T1 set colx=(select colx from T2) </code> but this is syntactically wrong as subquery returns multiple rows,
2
5332
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like what hotmail web UI is doing now (having the option of selecting multiple rows (using the checkbox provided) and perform a set of operations on them)
0
8366
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2779
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 we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.