This question already has an answer here:
How FOR XML PATH('') works when concatenating rows
(1 answer)
Closed 4 years ago.
I have a SQL Server output which returns the following values with one column:
RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
Output :
RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
need to concatenate all the rows in to one row.
use xml path like below
SELECT STUFF((
SELECT
','+cast(representid as varchar(30))
FROM
table
FOR
XML PATH('')
),1,1,''
) AS comma_seperated_list;
On sql2017 , you can use string_aggregate like below
select string_agg(cast(representid as varchar(40)),',')
from table
Related
This question already has answers here:
How can I get column names from a table in SQL Server?
(22 answers)
Closed 6 months ago.
I have a table name as 'tblOrder', having three columns. I would like to print the column names.
I want a script that should return a result as:
OrderId Name OrderTrackingNo
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('tblOrder')
Try this
This question already has answers here:
SQL Server reducing the length of the string to 8000 characters
(2 answers)
Closed 2 years ago.
In the following example, the txt column length is not correctly shown.
CREATE TABLE dbo.test
(
Id int IDENTITY,
txt varchar(max)
);
insert into test(txt) select REPLICATE('0',8000);
insert into test(txt) select REPLICATE('00',8000);
select id, len(txt) from test;
drop table test;
The result shown for both were 8000.
Could any body help?
If you want to insert something into a varchar(max) column - your inserting code needs to explicitly convert it to varchar(max) - try this:
insert into test(txt) select REPLICATE(cast('00' as varchar(max)), 8000);
That should give you a string of 16'000 characters length in the table.
This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 5 years ago.
I have a table that currently looks like what is below. I'd like to only have one row per date and then have each EditType value be a separate column with the count so I can easily view the different types of edits based on the day. Can this be done using a pivot?
Or... would it be better to back up a step before I've grouped the rows in order to get the counts, like what is shown below?
The desired output would be something like this:
SELECT
Gdb
, Tbl
, EditDate
, [I]
, [U]
, [D]
FROM #TABLE
PIVOT (
COUNT(EditType)
FOR EditType IN ([U], [I], [D])
) AS PIVOTED
This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 5 years ago.
i want to get all the employee names in one cell in sqlserver. values should come with comma separated in one cell.
output
arvind
chang
deepak
desired output
Arvind, Chang, Deepak
query
select EmpName from Employee
SELECT STUFF((SELECT ', ' + upper(left(EmpName,1)) + substring(EmpName,2,len(EmpName))
FROM Employee
FOR XML PATH('')) ,1,1,'') AS EmpName
I hope it will work for you.
This question already has answers here:
How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field? [duplicate]
(13 answers)
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 7 years ago.
I have a table with the following structure:
PERSON Sequence TEXT
1 1 John ran
1 2 across the field
2 1 Bill also
2 2 ran across the field after John
I would like to write a query that produces the following result:
PERSON TEXT
1 John ran across the field
2 Bill also ran across the field after John
Is something like this possible with T-SQL
Quick sketch:
SELECT DISTINCT ST2.person,
SUBSTRING(
(
SELECT ' '+ST1.[text] AS [text()]
FROM [dbo].[tab] ST1
WHERE ST1.person = ST2.person
ORDER BY ST1.person, ST1.sequence
FOR XML PATH ('')
), 2, 1000) AS [text]
FROM [dbo].[tab] ST2
For anyone who wants to play with it
Try this
SELECT DISTINCT PERSON, STUFF((SELECT ' ' + TEXT
FROM [YourTable] a
WHERE a.PERSON = b.PERSON
ORDER BY PERSON,SEQUENCE
FOR XML PATH('')),1,1,'') as TEXT
FROM [YourTable] b