简体   繁体   English

SQL Server 2019 查询在尝试将单个列分成多列数据时返回所有 null 值

[英]SQL Server 2019 query returns all null values when trying to separate a single column into multiples columns of data

I have a table of data that uses a / as delimiter.我有一个使用/作为分隔符的数据表。 I'm trying to separate the single column of data into three columns.我正在尝试将单列数据分成三列。

My query is returning the proper number of rows, but all three columns are NULL.我的查询返回了正确的行数,但所有三列都是 NULL。 Does anything jump off the page as obvious that I've missed?是否有任何我错过的明显内容跳出页面?

Here is how the data is stored in the PGroup column:以下是数据在PGroup列中的存储方式:

Steve/Mouse/Vitamin
Matt/Cat/Soda
Shelly/Dog/Bread

I'm hoping to return 3 separate columns: Name, Pet, Food我希望返回 3 个单独的列: Name, Pet, Food

DECLARE @delimiter VARCHAR(50)
SET @delimiter='/' 

;WITH CTE AS
(
    SELECT 
        CAST('' + REPLACE([PGroup], @delimiter, '') + '' AS XML) AS [PXML]
    FROM 
        xx123
)
SELECT
    [PXML].value('/M[1]', 'varchar(50)') As [Name],
    [PXML].value('/M[2]', 'varchar(50)') As [Pet],
    [PXML].value('/M[3]', 'varchar(50)') As [Food]
FROM 
    CTE

Please try the following solution.请尝试以下解决方案。

Notable points:值得注意的点:

  • CData section protects against XML entities like ampersand and the like. CData 部分可防止 XML 实体(如&符号等)。
  • XPath expressions were adjusted to use text() for performance reasons.出于性能原因,将 XPath 表达式调整为使用text()

SQL SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, tokens VARCHAR(1000));
INSERT INTO @tbl (tokens) VALUES
('Steve/Mouse/Vitamin'),
('Matt/Cat/Soda'),
('Shelly/Dog/Bread');
-- DDL and sample data population, end

DECLARE @separator CHAR(1) = '/';

SELECT ID
    , c.value('(/root/r[1]/text())[1]', 'varchar(50)') As [Name]
    , c.value('(/root/r[2]/text())[1]', 'varchar(50)') As [Pet]
    , c.value('(/root/r[3]/text())[1]', 'varchar(50)') As [Food]
FROM @tbl
CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' + 
        REPLACE(tokens, @separator, ']]></r><r><![CDATA[') + 
        ']]></r></root>' AS XML)) AS t(c);

Output Output

+----+--------+-------+---------+
| ID |  Name  |  Pet  |  Food   |
+----+--------+-------+---------+
|  1 | Steve  | Mouse | Vitamin |
|  2 | Matt   | Cat   | Soda    |
|  3 | Shelly | Dog   | Bread   |
+----+--------+-------+---------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单个 SQL 查询在数据库的所有列中查找 null 个值 - Single SQL query to find null values in all columns in a data base SQL Server:查询以将特定列的所有行值转换为单独的列 - SQL Server : query to convert all row values of a particular column into separate columns 选择多列时,SQL查询返回值,但仅选择一列时,无数据返回 - SQL query returns values when selecting multiple columns, but no data when selecting only one column 将 XML 数据导入 SQL 2019 只返回 NULL 或空值 - Importing XML data to SQL 2019 only returns NULL or empty values Sql 查询将所有列合并为单个列 - Sql query to merge all columns into a single column SQL Server查询提供所有空值 - SQL Server query excude all null values 尝试将多个键 ID 组合成单行,但列中的值不同 - Trying to combine multiples of a key ID into single row, but with different values in columns SQL查询,用于在同一列上将相关值显示为单独的列 - SQL query for displaying related values on same column as separate columns 尝试在sql server中将&#39;NULL&#39;的所有字符串值更新为NULL - Trying to update all string values of 'NULL' to NULL in sql server 在单个更新查询中更新多个列以自动堆叠SQL Server 2008中的列值 - Update multiple columns in a single update query to autostack column values in sql server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM