Solved

create a semicolon delimeted list

Posted on 2016-09-01
3
36 Views
Last Modified: 2016-09-07
I want to insert into a table a semicolon delimeted values.

table 1 : userid
row 1      prs
row 2      ppp

so I do a select on table

select userid from table1

the resultset that i should get should be : prs;ppp

which I will go ahead and insert in table2 useridlist column

table2
                   useridlist
row1        prs;ppp
0
Comment
Question by:PratikShah111
3 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41780988
try like this.
declare @table table (userid varchar(100))
insert @table values ('prs'),('ppp')
select  STUFF(( SELECT ','+CONVERT(VARCHAR,userid)
                FROM  @table 
                 FOR XML PATH('')), 1, 1, '') useridlist
-- prs,ppp

Open in new window

0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41781005
Mine looked almost identical ...
SELECT TOP (5) p.Name AS ProductName
, STUFF((SELECT TOP (5) ', ' + CAST(t.TransactionID AS VARCHAR(20)) 
FROM [AdventureWorks2014].[Production].[TransactionHistory] t 
WHERE T.ProductID = p.ProductID FOR XML PATH('')), 1, 1, '') as TransactionIds 
FROM [AdventureWorks2014].[Production].Product p 

Open in new window

FOR XML PATH
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41781058
Such data is a bad idea, if you want to store userlists into a database, you'd have this design:

a) Users
ID, NAME

b) UserLists
ID, NAME

c) UsersInUserlists
ID, UserListID, UserID

To get a list of users:

SELECT Users.* FROM UserLists 
INNER JOIN UsersInUserLists On UsersInUserLists.UserListID = UserLists.ID
INNER JOIN Users ON Users.ID = UsersInUserLists.UserID
WHERE UserList.Name = 'list name' -- or if you know an ID value: WHERE UserList.ID = 3

Open in new window


This type of design is not just a theoretical idealized form of storing data. It is best practice. The UsersInUserlists can also be called an n:m relation, it's there to assign users to userlists or vice versa assign userlists to users.

Bye, Olaf.
1

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 27
SQL server 2008 SP4 29 34
SQL Split character from numbers 3 18
shrink datafile Sql server 4 18
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now