【问题标题】:SQL, remove appearances of a comma at end of the lineSQL,删除行尾出现的逗号
【发布时间】:2010-12-13 19:23:55
【问题描述】:

我的数据如下所示

MyText
-------
some text, some more text, even more text,,
some text,,,,
some text, some text,,,
some text, some more text, even more, yet more, and again

我想实现:

MyText
-------
some text, some more text, even more text
some text
some text, some text
some text, some more text, even more, yet more, and again

如何删除行尾的逗号?我必须保留项目之间的逗号,但我需要从末尾删除任何逗号

我需要在 select 语句中执行此操作,但我无法找到在不编写函数的情况下应用 RegEx 的解决方案(我希望避免这样做)

我有一个解决方案,但它特别脏,我想改进它。我正在使用一组嵌套的 REPLACE 将 4 个逗号替换为 3,将 3 替换为 2,将 2 替换为一个,然后删除结尾的一个

有什么想法吗?

编辑:数据来自外部系统,所以我无法控制,否则我会在第一个实例中正确地用逗号连接。我使用的这条语句将在 SQL Server 2005 上运行

【问题讨论】:

  • 你使用的是什么数据库服务器?
  • 对不起,忘了说。 SQL Server 2005。谢谢 :)
  • 一开始为什么会有逗号?
  • 数据来自另一个系统,我无法控制。

标签: sql sql-server replace


【解决方案1】:

可能有更好的解决方案,但作为最后的手段,您可以使用这样的循环:

while RIGHT(@theString, 1)=','
  select @theString=SUBSTRING(@theString, 1, LEN(@theString)-1)

(这至少适用于 SQL Server)

【讨论】:

    【解决方案2】:

    我认为编写一个 sclar 函数对你来说是一个“干净”的解决方案。

    调用你的函数,比如RemoveCommasAtEnd(sqlLine)

    您只需在您的选择语句中调用它。 在函数体中,放入逻辑以删除逗号。如果您需要这方面的帮助,请告诉我们

    【讨论】:

    • 如果可以选择,我必须同意这是最整洁的解决方案。
    【解决方案3】:

    试试这样的

    DECLARE @Table TABLE(
            Val VARCHAR(MAX)
    )
    
    INSERT INTO @Table (Val)  SELECT 'some text, some more text, even more text,,'
    INSERT INTO @Table (Val)  SELECT 'some text,,,,'
    INSERT INTO @Table (Val)  SELECT 'some text, some text,,,'
    INSERT INTO @Table (Val)  SELECT 'some text, some more text, even more, yet more, and again'
    
    SELECT  Val,
            REVERSE(SUBSTRING(  REVERSE(Val),   PATINDEX('%[A-Za-z0-9]%',REVERSE(Val)), LEN(VAL) - (PATINDEX('%[A-Za-z0-9]%',REVERSE(Val)) - 1) ) ),
            *
    FROM    @Table
    

    【讨论】:

    • 把这个建议放到函数 RemoveCommasAtEnd(sqlLine) 中,你就有了一些看起来不错,而且工作得体的东西。
    【解决方案4】:

    我正在使用一组嵌套的 REPLACE 来 用 3 替换 4 个逗号,用 2 替换 3,以及 2个一个

    以下要求一组恰好三个嵌套的 REPLACE 将任意数量的多个逗号 (CHAR(44)) 字符“压缩”为单个字符:

    WITH MyTable (MyText)
    AS
    (
     SELECT 'some text, some more text, even more text,,'
     UNION ALL
     SELECT 'some text,,,,'
     UNION ALL
     SELECT 'some text, some text,,,'
     UNION ALL
     SELECT 'some text, some more text, even more, yet more, and again'
    )
    SELECT REPLACE(
                    REPLACE(
                            REPLACE(
                                    MyText,CHAR(44), CHAR(44) + CHAR(22)
                                   ), CHAR(22) + CHAR(44), ''
                          ), CHAR(22), ''
                  ) AS MyText_commas_squeezed
      FROM MyTable;
    

    现在你只需要修剪你已经知道如何做的任何尾随单个逗号:)

    【讨论】:

      【解决方案5】:

      解决方案仅用作单一 REVERSE。

      SUBSTRING(myText, 1 , LEN(myText) - PATINDEX('%[A-Za-z0-9]%', REVERSE(myText)) + 1 )

      演示:

      WITH MyTable (MyText)
      AS( 
                    SELECT 'some text, some more text, even more text,,' 
          UNION ALL SELECT 'some text,,,,' 
          UNION ALL SELECT 'some text, some text,,,' 
          UNION ALL SELECT 'some text, some more text, even more, yet more, and again'
        )
      
      SELECT SUBSTRING(myText, 1 , LEN(myText) - PATINDEX('%[A-Za-z0-9]%', REVERSE(myText)) + 1 ) from MyTable
      

      【讨论】:

        【解决方案6】:

        试试这个 - 它类似于给定的答案,并具有支持文本中其他字符的额外好处(一些文本,一些文本!,,,在给定的解决方案中无法正常工作)。它也有点短。

        SELECT *, LEFT(val, LEN(val) - PATINDEX('%[^,]%', reverse(val)) + 1) FROM #TempTbl
        

        作为参考,您在反转的字符串中找到第一个非逗号字符,然后将初始字符串右修剪那么多地方。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-25
          • 2011-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-13
          相关资源
          最近更新 更多