SQL Server删除一个字符串中的多个字符串

时间:2017-08-17 14:13:55

标签: sql sql-server stored-procedures

我的数据库中有以下文本字符串:

Value1 - Value2:  Value3 - Value4: Value5 - Value6: 

我需要删除破折号和破折号之间的所有内容直到冒号

上述结果将成为:

Value1: Value3: Value5:

基本上,可能会有无穷无尽的数值,但只能有一系列值。

注意事项:值可以是任何字符串!

有一种简单的方法吗?优选地,没有UDF。任何人都可以帮我解决这个问题吗?提前谢谢!

编辑:我同意这是一个非常差的实现。数据库本身的其余部分根本不是这样的。这只是一张桌子。我从中获得的查询将用于视图中,其中所有值都分成多个别名。感谢您的理解

2 个答案:

答案 0 :(得分:1)

您可以使用拆分功能......虽然您的值不应该首先以这样的方式存储。

declare @table table (col1 varchar(256))
insert into @table
values
('Value1 - Value2:  Value3 - Value4: Value5 - Value6:')

select
    ReturnVal = replace(ltrim(left(Item,charindex('-',Item))),'-',':')
from
    @table
    cross apply dbo.DelimitedSplit8K(col1,':')
where
    Item <> ''

<强>返回

+-----------+
| ReturnVal |
+-----------+
| Value1 :  |
| Value3 :  |
| Value5 :  |
+-----------+

或者,一个丑陋的黑客将它恢复到你想要的状态

select distinct
    --ReturnVal = replace(ltrim(left(Item,charindex('-',Item))),'-',':')
    ReturnVal = 'V' + STUFF((
          SELECT replace(left(Item,charindex('-',Item)),'-',':')
            FROM
            @table
            cross apply dbo.DelimitedSplit8K(col1,':')
     FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from
    @table
    cross apply dbo.DelimitedSplit8K(col1,':')
where
    Item <> ''

<强>返回

ReturnVal
Value1 :  Value3 : Value5 :

<强> JEFF MODEN SPLITTER

CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!

RETURNS TABLE WITH SCHEMABINDING AS
RETURN

/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
enough to cover VARCHAR(8000)*/

  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;
GO

答案 1 :(得分:0)

SELECT substring(NameValue, 1, charindex('_', NameValue)-1) AS Names, 
  substring(NameValue, charindex('_', NameValue)+1, LEN(NameValue)) AS Values
FROM Table

编辑:这样的函数或存储过程结合临时表应该适用于多行,具体取决于行分隔符,你还应该在开始之前删除CHAR(13):

DECLARE @helper varchar(512)
DECLARE @current varchar(512)
SET @helper = NAMEVALUE
WHILE CHARINDEX(CHAR(10), @helper) > 0 BEGIN
    SET @current = SUBSTRING(@helper, 1, CHARINDEX(CHAR(10), NAMEVALUE)-1)
    SELECT SUBSTRING(@current, 1, CHARINDEX('_', @current)-1) AS Names, 
      SUBSTRING(@current, CHARINDEX('_', @current)+1, LEN(@current)) AS Names
    SET @helper = SUBSTRING(@helper, CHARINDEX(CHAR(10), @helper)+1, LEN(@helper))
END
SELECT SUBSTRING(@helper, 1, CHARINDEX('_', @helper)-1) AS Names, 
  SUBSTRING(@helper, CHARINDEX('_', @helper)+1, LEN(@helper)) AS Names