需求是這樣的,須要將數據庫中的支付方式列(用「;」分割的字符串)按支付方式拆分;html
首先參考博客園split的文章,我採用方法2,sql
IF EXISTS ( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[f_splitSTR]') AND xtype IN ( N'FN', N'IF', N'TF' ) ) DROP FUNCTION [dbo].[f_splitSTR]; GO --方法1:循環截取法 CREATE FUNCTION f_splitSTR ( @s VARCHAR(8000) , --待分拆的字符串 @split VARCHAR(10) --數據分隔符 ) RETURNS @re TABLE ( col VARCHAR(100) ) AS BEGIN DECLARE @splitlen INT; SET @splitlen = LEN(@split + 'a') - 2; WHILE CHARINDEX(@split, @s) > 0 BEGIN INSERT @re VALUES ( LEFT(@s, CHARINDEX(@split, @s) - 1) ); SET @s = STUFF(@s, 1, CHARINDEX(@split, @s) + @splitlen, ''); END; INSERT @re VALUES ( @s ); RETURN; END; GO
具體分割:數據庫
SELECT* FROM BPM_EnterpriseOrgActivity e CROSS APPLY f_splitSTR(e.PaymentId,';')