仅删除前导或尾随回车 [英] Remove only leading or trailing carriage returns
问题描述
我很惊讶这个问题还没有被有意义地提出.如何在 SQL 中创建一个等效的函数,例如 LTRIM
或 RTRIM
用于仅在字符串的开头或结尾处回车和换行.
I'm dumbfounded that this question has not been asked meaningfully already. How does one go about creating an equivalent function in SQL like LTRIM
or RTRIM
for carriage returns and line feeds ONLY at the start or end of a string.
显然 REPLACE(REPLACE(@MyString,char(10),''),char(13),'')
删除所有回车和换行符.这不是我要找的.我只想删除前导或尾随.
Obviously REPLACE(REPLACE(@MyString,char(10),''),char(13),'')
removes ALL carriage returns and new line feeds. Which is NOT what I'm looking for. I just want to remove leading or trailing ones.
推荐答案
找出第一个非的字符 CHAR(13)
或 CHAR(10)
并从字符串的长度中减去它的位置.
Find the first character that is not CHAR(13)
or CHAR(10)
and subtract its position from the string's length.
LTRIM()
SELECT RIGHT(@MyString,LEN(@MyString)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',@MyString)+1)
RTRIM()
SELECT LEFT(@MyString,LEN(@MyString)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',REVERSE(@MyString))+1)
这篇关于仅删除前导或尾随回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!