我有一个存储在SQL中的字符串,存储在@result
变量中,类型为Varchar(MAX)
,内容如下:
Attributes >> Items >> Category >> Package
如何在不影响存储过程性能的情况下获取其反向结果。我想根据>>
分隔符拆分字符串。
Package >> Category >> Items >> Attributes
我有一个存储在SQL中的字符串,存储在@result
变量中,类型为Varchar(MAX)
,内容如下:
Attributes >> Items >> Category >> Package
如何在不影响存储过程性能的情况下获取其反向结果。我想根据>>
分隔符拆分字符串。
Package >> Category >> Items >> Attributes
如果字符串中最多有四个项目且没有包含句点,则可以使用PARSENAME()
函数。
select (parsename(replace(@result, ' >> ', '.'), 1) + ' >> ' +
parsename(replace(@result, ' >> ', '.'), 2) + ' >> ' +
parsename(replace(@result, ' >> ', '.'), 3) + ' >> ' +
parsename(replace(@result, ' >> ', '.'), 4)
)
>>
将字符串拆分为不同的部分,并为每行分配行号。>>
按降序排序将行转换为单个字符串。DECLARE @output VARCHAR(5000)='',
@String VARCHAR(5000)='Attributes >> Items >> Category >> Package'
SELECT @output += splt_data + ' >> '
FROM (SELECT Row_number()
OVER(
ORDER BY (SELECT NULL)) rn,
Rtrim(Ltrim(Split.bb.value('.', 'VARCHAR(100)'))) splt_data
FROM (SELECT Cast ('<M>' + Replace(@String, '>>', '</M><M>')
+ '</M>' AS XML) AS Data) AS A
CROSS APPLY Data.nodes ('/M') AS Split(bb)) ou
ORDER BY rn DESC
OUTPUT :
SELECT LEFT(@output, Len(@output) - 3) --Package >> Category >> Items >> Attributes
Select Reverse(@result)