473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MS SQL Server Multiple Rows into 1 column.. Semicolon Delimited.. Null Value Problem

I have a question. I am working on something based on the information I got from this forum.

I am using the sample I found and modifying it a little to get the results I want..

--TABLE--
Create table test
(PersonID varchar(2),
Degree varchar(10))

insert into test values ('55', 'MD')
insert into test values ('55', 'PhD')
insert into test values ('55', 'RN')
insert into test values ('60', 'MD')
insert into test values ('60', 'PhD')

--SO FAR THIS IS WHAT I HAVE

declare @DegName varchar(50)
declare @Sql nvarchar(4000)

declare c cursor FAST_FORWARD for
select distinct degree from test order by degree

open c
fetch next from c into @DegName

set @Sql = 'select personid, '
while @@Fetch_Status = 0
begin
set @Sql = @Sql + '+ Min(Case when Degree = ''' + @DegName + ''' then
degree end) + ''; '' '
fetch next from c into @DegName
end
close c
deallocate c
set @Sql = @Sql + 'as Degree'
print @sql
set @Sql = @Sql + ' from test group by PersonId '
print @sql
exec (@sql)

--DESIRED RESULTS--
PersonID Degree
55 MD; PhD; RN
60 MD; PhD

--ACTUAL RESULTS--
PersonID Degree
55 MD; PhD; RN
60 NULL <--- I need help in changing this to MD; PhD

I am only using SQL Server 2000.. I am open to other methods as long as the Desired result is achieved
Feb 20 '08 #1
4 3778
ck9663
2,878 Expert 2GB
I did not read the rest of your code after this:

Expand|Select|Wrap|Line Numbers
  1. select distinct degree from test order by degree
Don't you think you should have a list of distinct PersonId and not degree? From the look of your desired result, you want all rows from your test table into 1 row if they all have the same PersonId. You have a list of distinct degree, which could have multiple PersonId.Should it be the other way around?

-- CK
Feb 20 '08 #2
I did not read the rest of your code after this:

Expand|Select|Wrap|Line Numbers
  1. select distinct degree from test order by degree
Don't you think you should have a list of distinct PersonId and not degree? From the look of your desired result, you want all rows from your test table into 1 row if they all have the same PersonId. You have a list of distinct degree, which could have multiple PersonId.Should it be the other way around?

-- CK
ok. do you have an idea on how you are going to do it?

I already made a research, the desired result is not obtainable using a simple Select Statement..
Feb 20 '08 #3
please help.. I got this Dynamic SQL here but I tweaked it a little bit. Also, there is a group by personID in the statement.
Feb 22 '08 #4
Try this method

DECLARE @EmployeeList varchar(8000)

SELECT @EmployeeList = COALESCE(@EmployeeList + ', ', '') + FirstName

FROM (
select 'Jose' as FirstName
union
select 'Haprise' as FirstName
)aa

SELECT @EmployeeList

BR
Ramkumar
Feb 29 '08 #5

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

Similar topics

8
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
0
by: Jim French | last post by:
I have a page that takes a comma delimited string and needs to bind to a Repeater control. The string is split into an Array, and each value is put into a DataBase query via a For Each loop. The...
3
by: Dan | last post by:
I'm writing a record from an asp.net page to SQL Server. After the insert I'm selecting @@identity to return the ID of the record that I just wrote. It worked fine until I typed a semicolon into...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
9
by: billmiami2 | last post by:
I was playing around with the new SQL 2005 CLR functionality and remembered this discussion that I had with Erland Sommarskog concerning performance of scalar UDFs some time ago (See "Calling...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
0
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...

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.