How to concatenate multiple rows into one field in sql server [duplicate] - sql

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 8 years ago.
Using simple query , I can do something like
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
and get:
shopping
fishing
coding
but instead I just want 1 row, 1 col:
shopping, fishing, coding
for ref-- Can I concatenate multiple MySQL rows into one field?
I want to do this in sql server ??

SQL Server doesn't have great support for aggregate string concatenation. But you can do:
select stuff((select ', ' + hobbies
from peoples_hobbies
where person_id = 5
for xml path ('')
), 1, 2, '') as hobbies;

Related

ANSI SQL : SPLIT column value INTO ROWS based on aspecific character [duplicate]

This question already has answers here:
Split a CSV field into different rows in SQL
(3 answers)
Closed 2 years ago.
I want to transpose different values within a column separated by a specific character without involving PIVOT function
For the below example different set of courses are separated by semi-colon. The intention is to create different rows whenever it will find a semi-colon. I am not sure how many different set of values will be there under courses separated by semi-colon.
The solution I am looking for using ANSI SQL.
Course_Year Courses
2004 A|B|C|D;E|F|G|H
2005 A1|B1|C1|D1
2006 X1|X2|X3|X4;Y1|Y2|Y3|Y4;Z1|Z2|Z3|Z4
Output I am looking for
Course_Year Course
2004 A|B|C|D
2004 E|F|G|H
2005 A1|B1|C1|D1
2006 X1|X2|X3|X4
2006 Y1|Y2|Y3|Y4
2006 Z1|Z2|Z3|Z4
If you are using sql server then you can easily achieve that with stuff and for xml path():
select distinct t1.Course_Year,
STUFF(
(SELECT '; ' + convert(varchar(10), t2.Course, 120)
FROM courses t2
where t1.Course_Year = t2.Course_Year
FOR XML PATH (''))
, 1, 1, '') AS Courses
from courses t1;
Output:
To transform ';' separated strings from a column to row you can use strin_split() function with cross apply in sql server as below:
select Course_Year,value course from course cross apply string_split(courses,';')
Input:
Output:

extract word from string in sql server [duplicate]

This question already has answers here:
Find a specific substring using Transact-SQL
(6 answers)
Closed 5 years ago.
I need to extract part of a string in sql server. Lets say I have this string in a column...
Name1=Bill Gates&Name2=Microsoft&Address1=The streetadress
How can I extract the text that is equal to Name2 eg Microsoft?
declare #string varchar(100) = 'Name1=Bill Gates&Name2=Microsoft&Address1=The streetadress'
select replace(PARSENAME (replace(#string, '&', '.'), 2), 'Name2=', '');
One old fashioned way of handling this is to just use basic string functions like CHARINDEX and SUBSTRING.
SELECT
SUBSTRING(col,
CHARINDEX('Name2', col) + 6,
CHARINDEX('&', col, CHARINDEX('Name2', col)) -
CHARINDEX('Name2', col) - 6) AS name
FROM yourTable
Note that this solution assumes that the key value pairs are fixed in the order you showed us. My query uses the ambersand after the second name as a termination marker. If this be not present, e.g. if Name2 could possibly the last key in the string, then my query would have to be updated.
Demo here:
Rextester

How to query for a specific part of a string field with SQL? [duplicate]

This question already has answers here:
How to get substring in SQLIte?
(3 answers)
Closed 7 years ago.
Is it possible to build a query/SELECT-Statement with SQL like that:
SELECT name
FROM MyTable
WHERE name[2] = 'x'
WHERE substr(name, 2, 1) = 'x'
SQLFiddle demo

Split data from one colunn into two new columns in SQL on multiple rows [duplicate]

This question already has answers here:
How to split a comma-separated value to columns
(38 answers)
Closed 8 years ago.
I'm using SQL Express 2012, and I'm rather new to it so this website has been a blessing so far! I'm now stuck on a query that I've not found a suitable answer to.
I have a table called Claims Passed. In this I have a column called Client_Name, in this is a list of names, these contain first and second names split by a space (e.g John Smith). I've created two new columns, Client_First_Name and Client_Surname.
What I'm trying to do is get the get the first name in to first name column and the surname into the surname column.
I came across something like this but it was only for one row, not all the rows in one go.
How can i do this?
This is the basic syntax you need, assuming each name has only one first name:
UPDATE [Claims Passed]
SET Client_First_Name = SUBSTRING(Client_Name, 1, CHARINDEX(' ', Client_Name) - 1),
Client_Surname = SUBSTRING(Client_Name,CHARINDEX(' ', Client_Name) + 1, LEN(Client_Name)
The problem on your approach is when a client has more than one surname and one last name this won't work. You could try something like this:
INSERT INTO Client_First_Name VALUES(SELECT Client_Name LIKE '% %' THEN LEFT(Client_Name, Charindex(' ', Client_Name) - 1));
INSERT INTO Client_Surname VALUES(SELECT Client_Name LIKE '% %' THEN RIGHT(Client_Name, Charindex(' ', Reverse(Client_Name)) - 1));

SQL command to split by "/" [duplicate]

This question already has answers here:
How to split a comma-separated value to columns
(38 answers)
Closed 9 years ago.
I have a data column with values like this:
table: type
ID|Descriptions
1 |chair/table/plates/
2 |chair2/table2/plates2/
What will be the SQL command to split it by "/" ?
Expected output
ID|Description1|Description2|Description3|
1 |chair |table |plates
2 |chair2 |table2 |plates2
Try this
;WITH Split_Descr ([ID],[Descriptions], xmldescr)
AS
(
SELECT [ID],
[Descriptions],
CONVERT(XML,'<Descr><desc>'
+ REPLACE([Descriptions],'/', '</desc><desc>') + '</desc></Descr>') AS xmldescr
FROM Table1
)
SELECT [ID],
xmldescr.value('/Descr[1]/desc[1]','varchar(100)') AS Descr1,
xmldescr.value('/Descr[1]/desc[2]','varchar(100)') AS Descr2,
xmldescr.value('/Descr[1]/desc[3]','varchar(100)') AS Descr3
FROM Split_Descr
SQL FIDDLE DEMO