如何使用Sql Server删除特定字符 [英] How to remove particular character using Sql Server
本文介绍了如何使用Sql Server删除特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Sql服务器日@Message中有一个字符串。
Hi,
I am having a string in Sql server day @Message.
@Message='\nRAM\nROM\nCPU'
我的问题是我可以删除所有'\ n',将'\ n'替换为''(空字符串)
但是我想从上面提到的字符串中只删除第一次出现的'\ n'。
我试过
My problem is that I can remove all '\n' by replacing '\n' by ''(empty string)
But I want to remove only the first occurrence of '\n' from the above mentioned string.
I tried
select SUBSTRING (@Message ,0 , 2 )
但是我没有得到我的预期输出即'RAM\\\
ROM\\\
CPU'
But I don't get my expected out i.e 'RAM\nROM\nCPU'
推荐答案
如果始终以\ n开头,则此方法可以正常工作。但是子串需要2个整数值,起始位置和长度。所以,在你的例子中,你试图获得前2个字符,而实际上你想要在前2个字符之后的所有内容。
试试这个:
If it always starts with \n then this approach can work. However substring needs 2 integer values, starting position and length. So, in your example, you are trying to get the first 2 characters when in reality you want everything after the first 2 characters.
Try this instead:
SELECT SUBSTRING (@Message, 3, LEN(@Message))
试试这个:
Try this:
select RIGHT(@Message, len(@Message)-2)
了解更多: T-SQL:RIGHT,LEFT,SUBSTRING和CHARINDEX函数 [ ^ ]
SQL Server中的字符索引是从一开始的。尝试 SUBSTRING(@Message,1,2)
Character indices in SQL Server are one-based. Try SUBSTRING (@Message ,1 , 2 )
这篇关于如何使用Sql Server删除特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文