你可以使用PatternSplitCM(DDL下面的功能)。该解决方案是这样的(注意,您需要的SQL Server 2012+运行这一点,因为我使用LEAD):
declare @string varchar(255) =
'abcdefg 9/21/17 took the notes. 9/23/17 printed the notes. 9/21/17 took the notes. 9/23/17 printed the notes.'
select ItemNumber = concat(ItemNumber/2,':'), Item
from
(
select ItemNumber, item = item +' '+ LEAD(item, 1) OVER (ORDER BY itemNumber), [Matched]
from dbo.PatternSplitCM(@string, '[0-9/]')
) ExtractDates
where [Matched] = 1;
结果
ItemNumber Item
------------ ----------------------------
1: 9/21/17 took the notes.
2: 9/23/17 printed the notes.
3: 9/21/17 took the notes.
4: 9/23/17 printed the notes.
的功能
-- PatternSplitCM will split a string based on a pattern of the form
-- supported by LIKE and PATINDEX
--
-- Created by: Chris Morris 12-Oct-2012
ALTER FUNCTION [dbo].[PatternSplitCM]
(
@List VARCHAR(8000) = NULL
,@Pattern VARCHAR(50)
) RETURNS TABLE WITH SCHEMABINDING
AS
RETURN
WITH numbers AS (
SELECT TOP(ISNULL(DATALENGTH(@List), 0))
n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n))
SELECT
ItemNumber = ROW_NUMBER() OVER(ORDER BY MIN(n)),
Item = SUBSTRING(@List,MIN(n),1+MAX(n)-MIN(n)),
[Matched]
FROM (
SELECT n, y.[Matched], Grouper = n - ROW_NUMBER() OVER(ORDER BY y.[Matched],n)
FROM numbers
CROSS APPLY (
SELECT [Matched] = CASE WHEN SUBSTRING(@List,n,1) LIKE @Pattern THEN 1 ELSE 0 END
) y
) d
GROUP BY [Matched], Grouper;
SQL Server(SSMS) –
因此,每行都包含一个字符串,其中包含字符串中所有无意义的字符串,并且y你需要解析出它的大块?或者我误解了? – Xedni
假设所有的日期格式都是相似的,一种方法是使用递归CTE进行某种patindex解析。 – ZLK