码迷,mamicode.com
首页 > 数据库 > 详细

SQL拆分(转)

时间:2017-04-08 22:50:17      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:cte   str   and   app   ast   XML   dex   sub   char   

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拆分(转)

标签:cte   str   and   app   ast   XML   dex   sub   char   

原文地址:http://www.cnblogs.com/ppStudio/p/6683091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!