Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Consulta SQL para tornar uma coluna de números uma string

declare @List varchar(max)

select @List = isnull(@List + ',', '') + cast(ColumnName as varchar)
from MyTable

print @List

Por exemplo:
declare @List varchar(max)

select @List = isnull(@List + ',', '') + cast(object_id as varchar)
from sys.objects

print @List

ou
select stuff(
    (select ',' + cast(object_id as varchar)
    from sys.objects
    for xml path('')),
    1, 1, '')