一个字符串我在SQL字符串存储在Varchar(MAX)
型@result
变量是这样的:如何扭转在SQL Server
Attributes >> Items >> Category >> Package
我怎样才能得到这个反不影响存储过程的性能。我想打破>>
的基础上的字符串。
Package >> Category >> Items >> Attributes
一个字符串我在SQL字符串存储在Varchar(MAX)
型@result
变量是这样的:如何扭转在SQL Server
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)
)
另一种选择是拆分字符串并重构它。
感谢您的回答。这是否会影响性能? – 2014-12-02 13:11:59
无论何时您向程序添加任何类型的代码,都会影响性能。该解决方案速度非常快,因此影响应该很小,甚至不可能引人注意。 – 2014-12-02 14:32:17
如果您正在运行SQL Server 2008或更新,请尝试:
Select Reverse(@result)
程序包<<类别<<项目<<属性。这是输出吗? – knkarthick24 2014-12-02 13:18:24
egakcaP >> yrogetaC >> smetI >> setubirttA – 2014-12-02 13:18:46
他说这个字符串存储在他的变量中。如果他希望字符串反转,那么最简单的方法就是使用系统函数REVERSE(string_expression) – 2014-12-02 13:46:13
打破串入使用的分隔符>>
的分配ROW_NUMBER每一行的不同部分。
然后Convert the rows into single string
用>>
划分顺序desc。
,如果你有超过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
有没有在字符串中最四种元素? – 2014-12-02 13:05:42