仅删除前导或尾随回车 [英] Remove only leading or trailing carriage returns

查看:33
本文介绍了仅删除前导或尾随回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶这个问题还没有被有意义地提出.如何在 SQL 中创建一个等效的函数,例如 LTRIMRTRIM 用于仅在字符串的开头或结尾处回车和换行.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆