首页 > 代码库 > SQL拆分(转)

SQL拆分(转)

DECLARE @STRIN VARCHAR(MAX)

SET @STRIN =

    ‘87349+2015-06-10 10:38_2015-06-23 10:38+20.00

                 $109603+2015-06-16 10:38_2015-06-22 10:38+0.00‘;

 

WITH CTE AS(

         SELECT RTRIM(T2.V)              V,

                CHARINDEX(‘+‘, T2.V)     N1,

                CHARINDEX(‘_‘, T2.V)     N2,

                CHARINDEX(‘+‘, T2.V, CHARINDEX(‘+‘, T2.V) + 1)N3

         FROM   (

                    SELECT CAST(‘<V>‘ + REPLACE(@STRIN, ‘$‘, ‘</V><V>‘) + ‘</V>‘ AS XML)

                           VS

                )T1

                CROSS APPLY(

             SELECT N.V.value(‘.‘, ‘VARCHAR(100)‘)V

             FROM   T1.VS.nodes(‘/V‘)N(V)

         )T2

     )

     ,CTE2 AS(

         SELECT LEFT(V, N1 -1) AS [stuID],

                SUBSTRING(V, N1 + 1, N2 -N1 -1)[date1],

                SUBSTRING(V, N2 + 1, N3 -N2 -1)[date2],

                RIGHT(V, LEN(V) -N3)[FeeStand]

         FROM   CTE

     )

 

SELECT [stuID],

       [date],

       [FeeStand]

FROM   CTE2 UNPIVOT([date] FOR T IN ([date1], [date2]))U

SQL拆分(转)