V
V
vindiselpetr2021-12-08 14:44:04
SQL Server
vindiselpetr, 2021-12-08 14:44:04

How to combine text from multiple lines into one text line?

Table

AZone2Adapter].[dbo].[_data_OrganizationCustomerSpecialties] ( [Id], [OrganizationCustomerId], [SpecialtyId])
How to display all columns with a condition in a script.?
DECLARE @TextProduct NVARCHAR(MAX);
SELECT @TextProduct = ISNULL(@TextProduct + ', ','') + QUOTENAME([OrganizationCustomerId])
  FROM [AZone2Adapter].[dbo].[_data_OrganizationCustomerSpecialties]
  WHERE [OrganizationCustomerId]='1002775640209670'
SELECT @TextProduct AS TextProduct
If you write:
SELECT @TextProduct = ISNULL(@TextProduct + ', ','') + QUOTENAME([OrganizationCustomerId]),[Id]

that will be an error.
STRING_AGGdoes not work, only for SQL Server 2017

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2021-12-08
@vindiselpetr

CONCAT

DECLARE @TextProduct NVARCHAR(MAX);
SELECT @TextProduct = CONCAT_WS( ', ', @TextProduct, [OrganizationCustomerId])
  FROM [AZone2Adapter].[dbo].[_data_OrganizationCustomerSpecialties]
  WHERE [OrganizationCustomerId]='1002775640209670'
SELECT @TextProduct AS TextProduct

Microsoft SQL Server 2008

DECLARE @TextProduct NVARCHAR(MAX);
SELECT @TextProduct = STUFF(COALESCE(', ' + @TextProduct, '') 
                          + COALESCE(', ' + [OrganizationCustomerId], ''), 1, 2, '')
  FROM [AZone2Adapter].[dbo].[_data_OrganizationCustomerSpecialties]
  WHERE [OrganizationCustomerId]='1002775640209670'
SELECT @TextProduct AS TextProduct

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question