用于在两个已知字符串之间选择字符串的SQL查询

时间:2013-08-21 15:57:42

标签: sql sql-server substring

我需要一个SQL查询来获取两个已知字符串之间的值(返回的值应该以这两个字符串开头和结尾)。

一个例子。

“我所知道的只是那条狗非常糟糕,无论其他人怎么想都会立即受到严厉的惩罚。”

在这种情况下,已知的字符串是“狗”和“立即”。因此,我的询问应该返回“狗一直非常糟糕,需要立即进行严厉惩罚”

到目前为止,我已经想出了这个,但无济于事:

SELECT SUBSTRING(@Text, CHARINDEX('the dog', @Text), CHARINDEX('immediately', @Text))

@Text是包含主字符串的变量。

有人可以帮助我解决我出错的地方吗?

15 个答案:

答案 0 :(得分:43)

问题是你的substring参数的第二部分包括第一个索引。 您需要从第二个索引中减去第一个索引才能使其正常工作。

SELECT SUBSTRING(@Text, CHARINDEX('the dog', @Text)
, CHARINDEX('immediately',@text) - CHARINDEX('the dog', @Text) + Len('immediately'))

答案 1 :(得分:9)

一个例子是:你有一个字符串和字符$

字符串

aaaaa$bbbbb$ccccc

代码

SELECT SUBSTRING('aaaaa$bbbbb$ccccc',CHARINDEX('$','aaaaa$bbbbb$ccccc')+1, CHARINDEX('$','aaaaa$bbbbb$ccccc',CHARINDEX('$','aaaaa$bbbbb$ccccc')+1) -CHARINDEX('$','aaaaa$bbbbb$ccccc')-1) as My_String

输出

bbbbb

答案 2 :(得分:8)

我认为Evan的意思是:

SELECT SUBSTRING(@Text, CHARINDEX(@First, @Text) + LEN(@First), 
                 CHARINDEX(@Second, @Text) - CHARINDEX(@First, @Text) - LEN(@First))

答案 3 :(得分:5)

您需要在SUBSTRING中调整LENGTH。你把它指向'结束字符串'的结尾。

尝试这样的事情:

declare @TEXT varchar(200)
declare @ST varchar(200)
declare @EN varchar(200)
set @ST = 'the dog'
set @EN = 'immediately'
set @TEXT = 'All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought.'
SELECT SUBSTRING(@Text, CHARINDEX(@ST, @Text), (CHARINDEX(@EN, @Text)+LEN(@EN))-CHARINDEX(@ST, @Text))

当然,您可能需要稍微调整一下。

答案 4 :(得分:5)

SELECT 
SUBSTRING( '123@yahoo.com',  charindex('@','123@yahoo.com',1) + 1, charindex('.','123@yahoo.com',1) - charindex('@','123@yahoo.com',1) - 1 )

答案 5 :(得分:4)

试试这个并替换' [' &安培; ']'用你的字符串

SELECT SUBSTRING(@TEXT,CHARINDEX('[',@TEXT)+1,(CHARINDEX(']',@TEXT)-CHARINDEX('[',@TEXT))-1)

答案 6 :(得分:3)

我觉得您可能需要SQL Server PATINDEX()函数。看看这个:

Usage on Patindex() function

所以也许:

SELECT SUBSTRING(@TEXT, PATINDEX('%the dog%', @TEXT), PATINDEX('%immediately%',@TEXT))

答案 7 :(得分:2)

DECLARE @Text VARCHAR(MAX), @First VARCHAR(MAX), @Second VARCHAR(MAX)
SET @Text = 'All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought.'
SET @First = 'the dog'
SET @Second = 'immediately'

SELECT SUBSTRING(@Text, CHARINDEX(@First, @Text), 
                 CHARINDEX(@Second, @Text) - CHARINDEX(@First, @Text) + LEN(@Second))

答案 8 :(得分:2)

我有类似的需要解析存储在IIS日志的csUriQuery字段中的一组参数,如下所示:this format中需要id=3598308&user=AD\user&parameter=1&listing=No

我最终创建了一个用户定义的函数来完成之间的字符串,具有以下假设:

  1. 如果未找到起始事件,则返回NULL
  2. 如果未找到结束事件,则返回字符串的其余部分
  3. 以下是代码:

    CREATE FUNCTION dbo.str_between(@col varchar(max), @start varchar(50), @end varchar(50))  
      RETURNS varchar(max)  
      WITH EXECUTE AS CALLER  
    AS  
    BEGIN  
      RETURN substring(@col, charindex(@start, @col) + len(@start), 
             isnull(nullif(charindex(@end, stuff(@col, 1, charindex(@start, @col)-1, '')),0),
             len(stuff(@col, 1, charindex(@start, @col)-1, ''))+1) - len(@start)-1);
    END;  
    GO
    

    对于上述问题,用法如下:

    DECLARE @a VARCHAR(MAX) = 'All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought.'
    SELECT dbo.str_between(@a, 'the dog', 'immediately')
    -- Yields' had been very bad and required harsh punishment '
    

答案 9 :(得分:1)

你正在获得'惩罚立即'的起始位置,但是将其作为子串的长度参数传递。

你需要从'惩罚立即'的charindex中减去'狗'的起始位置,然后将'惩罚立即'字符串的长度添加到你的第三个参数。这样就会给你正确的文字。

这里有一些粗略的hacky代码来说明这个过程:

DECLARE @text VARCHAR(MAX)
SET @text = 'All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought.'

DECLARE @start INT
SELECT @start = CHARINDEX('the dog',@text)

DECLARE @endLen INT
SELECT @endLen = LEN('immediately')

DECLARE @end INT
SELECT @end = CHARINDEX('immediately',@text)
SET @end = @end - @start + @endLen

SELECT @end

SELECT SUBSTRING(@text,@start,@end)

结果: 这条狗非常糟糕,需要立即进行严厉处罚

答案 10 :(得分:0)

希望这会有所帮助: 声明一个变量,如果需要进行任何更改只需要一次。

声明@line varchar(100)

设置@line =' Email_i-Julie@mail.com'

选择SUBSTRING(@ line,(charindex(' - ',@ line)+1),CHARINDEX(' @',@ line)-charindex(&#39 ; - ',@线)-1)

答案 11 :(得分:0)

<pre>

DECLARE @text VARCHAR(MAX)

SET @text = 'All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought.'

declare @pretext as nvarchar(100) = 'the dog'    
declare @posttext as nvarchar(100) = 'immediately'

SELECT 
 CASE 
When CHARINDEX(@posttext, @Text) - (CHARINDEX(@pretext, @Text) + len(@pretext)) < 0 THEN 
''
Else
SUBSTRING(@Text, CHARINDEX(@pretext, @Text) + len(@pretext)
,   CHARINDEX(@posttext, @Text) - (CHARINDEX(@pretext, @Text) + len(@pretext)) )    
END as betweentext  

答案 12 :(得分:0)

SELECT SUBSTRING('aaaaa$bbbbb$ccccc',instr('aaaaa$bbbbb$ccccc','$',1,1)+1, instr('aaaaa$bbbbb$ccccc','$',1,2)-1) -instr('aaaaa$bbbbb$ccccc','$',1,1)) as My_String

答案 13 :(得分:0)

假设我们有一个字符串 DUMMY_DATA_CODE_FILE,我们想找出第 2 个和第 3 个下划线 (_) 之间的子字符串。然后我们使用这样的查询。

select  SUBSTRING('DUMMY_DATA_CODE_FILE',charindex('_', 'DUMMY_DATA_CODE_FILE', (charindex('_','DUMMY_DATA_CODE_FILE', 1))+1)+1, (charindex('_', 'DUMMY_DATA_CODE_FILE', (charindex('_','DUMMY_DATA_CODE_FILE',  (charindex('_','DUMMY_DATA_CODE_FILE', 1))+1))+1)- charindex('_', 'DUMMY_DATA_CODE_FILE', (charindex('_','DUMMY_DATA_CODE_FILE', 1))+1)-1)) as Code

答案 14 :(得分:-1)

select substring(@string,charindex('@first',@string)+1,charindex('@second',@string)-(charindex('@first',@string)+1))