SQL拆分多规则的字符串分离数字。

--拆分多规则字符串

DECLARE @Codes NVARCHAR(MAX)

SET @Codes = '6*BC-007,*BC-016,9*BC-015'

 --对于*BC-015这种情况,则Qty设置为1

SELECT  *

FROM    dbo.fun_split(@Codes, ',')



SELECT  (CASE WHEN CHARINDEX('*', sp.SplitValue) > 0

              THEN SUBSTRING(sp.SplitValue, CHARINDEX('*', sp.SplitValue) + 1,

                             LEN(sp.SplitValue) - CHARINDEX('*', sp.SplitValue))

              ELSE sp.SplitValue

         END) AS 'Code' ,

        (CASE WHEN CHARINDEX('*', sp.SplitValue) > = 2

              THEN SUBSTRING(sp.SplitValue, 1,

                             CHARINDEX('*', sp.SplitValue) - 1)

              ELSE 1

         END) AS 'Qty'

FROM    dbo.fun_split(@Codes, ',') AS sp

       

       

--SELECT  SUBSTRING('8*BC-015', CHARINDEX('*', '8*BC-015') + 1,

--                  LEN('8*BC-015') - CHARINDEX('*', '8*BC-015'))

       



--SELECT CHARINDEX('*','*BC-016') --返回是1

 

你可能感兴趣的:(sql)