Cuál es el equivalente de Cadena.¿Unirse a TSQL? [duplicar]
Posible Duplicado:
¿Hay alguna forma de crear una función de SQL Server para "unir" varias filas de una subconsulta en un solo campo delimitado?
Hola a todos!
Estoy buscando una manera fácil de concatenar valores 'n'. Algo como:
SELECT MyConcat(',', T.TextColumn)
FROM SomeTable AS T
WHERE T.SomeOtherColumn = SomeCondition
Así que si tengo una tabla como:
SomeTable:
Id | TextColumn | SomeOtherColumn
----+------------+----------------
1 | Qwerty | Y
2 | qwerty | N
3 | azerty | N
4 | Azerty | Y
Resultaría en algo como:
SQL:
SELECT MyConcat(';', T.TextColumn)
FROM SomeTable AS T
WHERE T.SomeOtherColumn = 'Y'
RESULT:
'Qwerty;Azerty'
35
2 answers
Esto debería hacer el truco:
DECLARE @Result VARCHAR(MAX);
SELECT
@Result = CASE
WHEN @Result IS NULL
THEN T.TextColumn
ELSE @Result + ';' + T.TextColumn
END
FROM
SomeTable AS T
WHERE
T.SomeOtherColumn = 'Y';
SELECT @Result
38
Author: domager,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-29 22:35:21
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-29 22:35:21
SELECT CAST(TextColumn + ';' AS VARCHAR(MAX))
FROM SomeTable
WHERE SomeOtherColumn = 'Y'
FOR XML PATH ('')
Si no te gusta el final ;
puedes eliminar el carácter del resultado.
EDITAR EN 2017
Muchas plataformas ahora soportan la función de ventana LISTAGG ()
26
Author: Hogan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-14 20:18:31
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-14 20:18:31