• John Mitchell-245523 - Monday, March 5, 2018 7:39 AM

    Number the rows in the StartPoint CTE using ROW_NUMBER().  Then, instead of using LEAD in the StartPointsandNext CTE, join StartPoint to itself on RowNo = RowNo + 1.

    John

    Hi John,
    I wonder if you can help me any further? I've joined on RowNo + 1 but now it misses off the 3rd Username. (It only brings back 2 records instead of 3)
    Here's the amended code....have you any ideas

    DECLARE @Contents varchar(4000)
    SET @Contents =
    'abcdef 01/01/2018 08:15:13 This is a note , this is a note , this is a note , this is a note , this is a note , this is a note , this is a note, ghijkl 02/02/2018 12:15:45, this is a note , this is a note , this is a note , this is a note mnopqr 03/02/2018 10:34:23 this is a note this is a note this is a note this is a note this is a note this is a note';

    WITH Ten AS (
      SELECT n FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (0)) v(n)
      )
    , Hundred AS (
      SELECT t1.n
      FROM Ten t1
      CROSS JOIN Ten t2
      )
    , Thousand(n) AS (
      SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
      FROM Hundred h
      CROSS JOIN Ten t
      )
    , StartPoints AS (
      SELECT n AS StartChar, ROW_NUMBER() OVER(ORDER BY n) AS Row#
      FROM Thousand
      WHERE SUBSTRING(@Contents,n,7) LIKE '[a-z][a-z][a-z][a-z][a-z][a-z] '
      AND ISDATE(SUBSTRING(@Contents,n+7,19)) = 1
      AND n<= LEN(@Contents) - 26
      )
    , StartPointssandNext AS (
      SELECT
       StartPoints.StartChar AS NextStartChar,
       sp2.StartChar AS StartChar
      FROM StartPoints
      JOIN StartPoints sp2 ON StartPoints.Row# = sp2.Row# + 1
      )
    SELECT
      SUBSTRING(@Contents,StartChar,NextStartChar-StartChar)
    FROM StartPointssandNext