SQL删除字符后面的下一个字母/字符^

时间:2016-03-27 09:46:30

标签: sql-server-2008

如何从表格中选定的行中删除字符^后的字符?

e.g。

TABLE Things 
Boat
Do^2gs
Cat^fs
^KBear
Mi^&ce
D^Rice

结果:

Boat
Dogs
Cats
Bear
Mice
Dice

3 个答案:

答案 0 :(得分:1)

select  case    when charindex('^', col) <> 0 
        then stuff(col, charindex('^', col), 2, '')
        else col 
        end

-- to handle multiple ^ up to max of 4

select  t.col,
        r4.col
from    Things t
    cross apply
    (
        select  col = case when charindex('^', col) <> 0 
            then stuff(col, charindex('^', col), 2, '')
            else col 
            end
    ) r1
    cross apply
    (
        select  col = case when charindex('^', r1.col) <> 0 
            then stuff(r1.col, charindex('^', r1.col), 2, '')
            else r1.col 
            end
    ) r2
    cross apply
    (
        select  col = case when charindex('^', r2.col) <> 0 
            then stuff(r2.col, charindex('^', r2.col), 2, '')
            else r2.col 
            end
    ) r3
    cross apply
    (
        select  col = case when charindex('^', r3.col) <> 0 
            then stuff(r3.col, charindex('^', r3.col), 2, '')
            else r3.col 
            end
    ) r4

-- UDF to remove the ^
create function remove_chr
(
    @str    varchar(100)
)
returns varchar(100)
as
begin
    while charindex('^', @str) <> 0 
    begin
        select  @str =  case    
                when charindex('^', @str) <> 0 
                then stuff(@str, charindex('^', @str), 2, '')
                else @str 
                end
    end
    return @str
end

答案 1 :(得分:0)

如果您使用MySQL,可以使用:

SELECT col, 
       IF(INSTR(col,'^') > 0,CONCAT(LEFT(col,INSTR(col, '^')-1),
          RIGHT(col,LENGTH(col) - INSTR(col, '^')-1)), col) AS result
FROM Things;

SqlFiddleDemo

SQL Server等价物:

SELECT col, 
       IIF(CHARINDEX('^',col) > 0,CONCAT(LEFT(col,CHARINDEX('^',col)-1),
              RIGHT(col,LEN(col) - CHARINDEX('^',col)-1)), col) AS result
FROM Things 

LiveDemo

SQL Server 2008

SELECT col, 
  CASE WHEN CHARINDEX('^',col) > 0 
    THEN LEFT(col,CHARINDEX('^',col)-1) + RIGHT(col,LEN(col) - CHARINDEX('^',col)-1)
    ELSE col
  END AS result
FROM Things;

请注意,只有^没有或只有 Column | Type | Modifiers ------------+-----------------------------+-------------------------------------------------------- id | integer | not null default nextval('merchants_id_seq'::regclass) name | character varying | not null nameb | jsonb | not null default '{}'::jsonb 时才会有效。

答案 2 :(得分:0)

这是解决方案,它可以消除任何数量的&#39; ^&#39; 。 我创建了一个函数SQL服务器,它不使用任何循环或游标。

CREATE  FUNCTION [dbo].[FnReplaceChar](@pOriginalText VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
        DECLARE @vText VARCHAR(1000)
                ,@vXML XML

        --Convert text as XML format
        SELECT @vXML = '<Root><dtl><f>' + REPLACE(@pOriginalText,'^','</f></dtl><dtl><f>^')+'</f></dtl></Root>'

        --Splits words started with '^' and combines after removing character starts with '^'
        SET @vText = (
                        SELECT  '' + ACT_TEXT
                        FROM
                        (
                        SELECT  CASE    WHEN CHARINDEX('^',DOC.COL.value('f[1]','VARCHAR(100)') ,0) > 0
                                        THEN STUFF(DOC.COL.value('f[1]','VARCHAR(100)'),CHARINDEX('^',DOC.COL.value('f[1]','VARCHAR(100)') ,0),2,'')
                                        ELSE DOC.COL.value('f[1]','VARCHAR(100)')
                                END AS ACT_TEXT
                        FROM    @vXML.nodes('/Root/dtl') DOC(COL)
                        )T
                        FOR XML PATH('')
                    )

        RETURN @vText
END

您可以在选择查询中使用此功能

SELECT dbo.[FnReplaceChar](col_Name) 
FROM [Things]