在SQL Server中将employee值行分隔为列可以使用多种方法,以下是其中的一种方法:
SELECT value AS employee
FROM STRING_SPLIT('John,Doe,Jane,Smith', ',');
这将把'John,Doe,Jane,Smith'字符串按逗号分隔为多个行,并将每个值作为单独的列返回。
DECLARE @xml XML;
SET @xml = '<employees><employee>John</employee><employee>Doe</employee><employee>Jane</employee><employee>Smith</employee></employees>';
SELECT
employee.value('.', 'VARCHAR(100)') AS employee
FROM
@xml.nodes('/employees/employee') AS employees(employee);
这将把XML字符串解析为XML对象,并使用XPath表达式从中选择employee节点作为列。
DECLARE @employees VARCHAR(MAX) = 'John,Doe,Jane,Smith';
WITH cte AS (
SELECT
LEFT(@employees, CHARINDEX(',', @employees) - 1) AS employee,
RIGHT(@employees, LEN(@employees) - CHARINDEX(',', @employees)) AS remaining
UNION ALL
SELECT
LEFT(remaining, CHARINDEX(',', remaining) - 1),
RIGHT(remaining, LEN(remaining) - CHARINDEX(',', remaining))
FROM
cte
WHERE
CHARINDEX(',', remaining) > 0
UNION ALL
SELECT
remaining,
''
FROM
cte
WHERE
CHARINDEX(',', remaining) = 0
)
SELECT employee
FROM cte;
这将使用递归CTE将字符串逐步拆分为多个行,并将每个值作为单独的列返回。
以上是在SQL Server中将employee值行分隔为列的几种常见方法。根据具体的场景和需求,选择适合的方法来实现分隔操作。
领取专属 10元无门槛券
手把手带您无忧上云