0

For example, I have a table with below values:

DECLARE @TEMP TABLE (NUMBER INT);
DECLARE @TEXT VARCHAR(MAX)

INSERT @TEMP VALUES (1), (2), (3), (4), (5)

SELECT * FROM @TEMP
NO
1
2
3
4
5

What I want is

NO
12345
Thom A
  • 88,727
  • 11
  • 45
  • 75
Janzen Eng
  • 43
  • 2
  • Does this answer your question? [How to use GROUP BY to concatenate strings in SQL Server?](https://stackoverflow.com/questions/273238/how-to-use-group-by-to-concatenate-strings-in-sql-server) – Charlieface Feb 07 '21 at 12:29
  • Maybe, you should search "string concatenation". Like that: https://stackoverflow.com/questions/3373100/t-sql-string-concatenation or like that https://stackoverflow.com/questions/194852/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-serv – Muzaffer Galata Feb 07 '21 at 12:30

2 Answers2

1

If you use MSSQL version later than 2013, you can do the following:

select STRING_AGG([NO],'') from @TEMP
Lev Gelman
  • 177
  • 8
1

if achieve the same using COALESCE function as well

DECLARE @TEMP TABLE (NUMBER INT);
DECLARE @TEXT VARCHAR(MAX)

INSERT @TEMP VALUES (1), (2), (3), (4), (5)


select @TEXT = COALESCE(@TEXT, '') + cast(NUMBER as nvarchar)
from @TEMP

select @TEXT
Praveen N
  • 120
  • 1
  • 7