SQL:删除字符串中的最后一个逗号 [英] SQL : remove last comma in string

查看:56
本文介绍了SQL:删除字符串中的最后一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL 表中有一个文本备注字段,如果它是逗号,我需要删除该字段中的最后一个字符.

I have a text memo field in SQL table that I need to remove the last character in the field if it's a comma.

例如,如果我有这些行,我需要从第 2 行和第 4 行中删除逗号.

So, for example, if I have these rows, I need to remove the commas from rows 2 and 4.

  INETSHORTD
1  94
2  85,
3  94, 92
4  89, 99, 32,

输出将是:

  INETSHORTD
  94
  85
  94, 92
  89, 99, 32

有什么想法吗?

推荐答案

使用REVERSESTUFF:

SELECT
    REVERSE(
        STUFF(
            REVERSE(LTRIM(RTRIM(INETSHORTD))), 
            1, 
            CASE WHEN SUBSTRING((REVERSE(LTRIM(RTRIM(INETSHORTD)))), 1, 1) = ',' THEN 1 ELSE 0 END, 
            ''
        )
    )
FROM tbl

<小时>

首先,您要TRIM 去除前导和尾随空格.然后REVERSE,检查第一个字符是否为,.如果是,删除它,否则什么都不做.然后 REVERSE 再次返回.您可以使用 STUFF(string, 1, 1, '') 删除第一个字符.


First, you want to TRIM your data to get rid of leading and trailing spaces. Then REVERSE it and check if the first character is ,. If it is, remove it, otherwise do nothing. Then REVERSE it back again. You can remove the first character by using STUFF(string, 1, 1, '').

SQL 小提琴

这篇关于SQL:删除字符串中的最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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