T-SQL - 在两个特定单词后拆分下一个单词

时间:2017-03-06 22:22:10

标签: sql sql-server tsql substring charindex

想象一下,我有以下字符串: "选择*来自table_1 INNER JOIN table_2 ON table_1.A = table_2.B"

我想提取语句的两个表以获得: 表格1 ; TABLE_2。

基本上我需要在字符串From和Join之后得到下一个单词。我尝试使用以下查询:

public void increment() {
    for (int i = BigInteger.length - 1; i; i--)
      if (BigInteger[i] < 9) {
        BigInteger[i]++;
        for (int j = i + 1; j < BigInteger.length; j++)
          BigInteger[j] = 0;
        break;
      }
      else if (i == 0)
        // cannot increment 9
}

public void decrement() {
    for (int i = BigInteger.length - 1; i; i--)
      if (BigInteger[i] > 0) {
        BigInteger[i]--;
        for (int j = i + 1; j < BigInteger.length; j++)
          BigInteger[j] = 9;
        break;
      }
      else if (i == 0)
        // cannot decrement 0
}

但我没有得到我想要的东西:(

任何人都可以帮助我?

非常感谢!

2 个答案:

答案 0 :(得分:2)

看看SQLZim的答案是完全有效(+1),让我意识到我已经厌倦了提取部分字符串。因此,我修改了我的解析函数以接受两个多字符分隔符。因此, IF 您对UDF持开放态度,请考虑以下事项:

旁注:作为一个表值函数,很容易合并到一个CROSS APPLY或作为单个查询

对于您的特定请求,我们正在寻找FROMON

的关键词
Declare @String varchar(max) = 'Select * From table_1 INNER JOIN table_2 ON table_1.A = table_2.B'
Select Table1 = left(RetVal,charindex(' ',RetVal+' ')-1)
      ,Table2 = right(RetVal,charindex(' ',reverse(RetVal)+' ')-1)
 From [dbo].[udf-Str-Extract] (@String,' from ',' on ')

<强>返回

Table1  Table2
table_1 table_2

通用示例:

让我们说我们在{}&#39>之间寻找价值

Declare @String varchar(max) = 'co-101{12345},co-513{22578}'
Select * From [dbo].[udf-Str-Extract] (@String,'{','}')

<强>返回

RetSeq  RetPos  RetLen  RetVal
1       8       5       12345
2       22      5       22578

另一个通用示例

Declare @String varchar(max) = '<root><firstmame>John</firstname><lastname>Cappelletti</lastname><phone>(401) 555-1212</phone></root>'
Select * From [dbo].[udf-Str-Extract] (@String,'>','<')

<强>返回

RetSeq  RetPos  RetLen  RetVal
1       18      4       John
2       44      11      Cappelletti
3       73      14      (401) 555-1212

感兴趣的UDF

CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table 
As
Return (  

with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
       cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
       cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
       cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)

Select RetSeq = Row_Number() over (Order By N)
      ,RetPos = N
      ,RetLen = charindex(@Delimiter2,RetVal)-1
      ,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1)
 From (Select A.N,RetVal = ltrim(rtrim(Substring(@String, A.N, A.L))) From cte4 A ) A
 Where charindex(@Delimiter2,RetVal)>1
)
/*
Max Length of String 1MM characters

Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/

答案 1 :(得分:1)

使用left()right(),charindex()的组合:

declare @str varchar(256) = 'Select * From table_1 INNER JOIN table_2 ON table_1.A = table_2.B';

select 
    tbl1 = left(right(@str,len(@str)-charindex(' From ',@str)-5)
        ,charindex(' ',right(@str,len(@str)-charindex(' From ',@str)-5))-1
       )
  , tbl2 = left(right(@str,len(@str)-charindex(' Join ',@str)-5)
        ,charindex(' ',right(@str,len(@str)-charindex(' Join ',@str)-5))-1
       )

rextester演示:http://rextester.com/WOO86939

返回:

+---------+---------+
|  tbl1   |  tbl2   |
+---------+---------+
| table_1 | table_2 |
+---------+---------+