如何删除 SQL Server 中的前导和尾随引号? [英] How can I remove leading and trailing quotes in SQL Server?

查看:41
本文介绍了如何删除 SQL Server 中的前导和尾随引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 数据库中有一个带有 NTEXT 列的表.此列可能包含用双引号括起来的数据.当我查询此列时,我想删除这些前导和尾随引号.

I have a table in a SQL Server database with an NTEXT column. This column may contain data that is enclosed with double quotes. When I query for this column, I want to remove these leading and trailing quotes.

例如:

这是一条测试消息"

应该变成

这是一条测试消息

我知道 LTRIM 和 RTRIM 函数,但这些仅适用于空格.关于我可以使用哪些功能来实现这一目标的任何建议.

I know of the LTRIM and RTRIM functions but these workl only for spaces. Any suggestions on which functions I can use to achieve this.

推荐答案

我刚刚在 MS SQL 2008 中测试了这段代码并进行了验证.

I have just tested this code in MS SQL 2008 and validated it.

删除最左边的引用:

UPDATE MyTable
SET FieldName = SUBSTRING(FieldName, 2, LEN(FieldName))
WHERE LEFT(FieldName, 1) = '"'

移除最右边的引用:(已修改以避免从隐式类型转换为 int 的错误)

Remove right-most quote: (Revised to avoid error from implicit type conversion to int)

UPDATE MyTable
SET FieldName = SUBSTRING(FieldName, 1, LEN(FieldName)-1)
WHERE RIGHT(FieldName, 1) = '"'

这篇关于如何删除 SQL Server 中的前导和尾随引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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