如何限制子字符串以提取SQL Server中两个空格之间的文本?

dja*_*ned 3 sql t-sql sql-server

如何限制子字符串以提取SQL Server中两个空格之间的文本?

2017-03-09 Today ABC.XYZ Work_In_Progress 
Run Code Online (Sandbox Code Playgroud)

输出应为:

ABC.XYZ
Run Code Online (Sandbox Code Playgroud)

我可以从第二个空格拉出,但不能限制到第三个空格:

SELECT ID, SUBSTRING(HISTORY, CHARINDEX(' ', HISTORY, CHARINDEX(' ', HISTORY) +1)+1,LEN(HISTORY)) 
from Test;
Run Code Online (Sandbox Code Playgroud)

Joh*_*tti 5

如果您知道要做什么,那么还有另一种选择。在这种情况下,第三个字符串

Declare @YourTable table (ID int,SomeColumn varchar(max))
Insert Into @YourTable values
(1,'2017-03-09 Today ABC.XYZ Work_In_Progress')


Select ID
      ,SomeValue = Cast('<x>' + replace(SomeColumn,' ','</x><x>')+'</x>' as xml).value('/x[3]','varchar(max)')
 From @YourTable
Run Code Online (Sandbox Code Playgroud)

退货

ID  SomeValue
1   ABC.XYZ
Run Code Online (Sandbox Code Playgroud)

XML安全版本

Select ID
      ,SomeValue = Cast('<x>' + replace((Select replace(SomeColumn,' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.').value('/x[3]','varchar(max)')
 From  @YourTable
Run Code Online (Sandbox Code Playgroud)