CREATE TABLE [dbo].[MM_T_MATER]([fbillno][NVARCHAR](50)NOT NULL,[fid][INT]NOT NULL,[FCREATEDATE][DATETIME]NOT NULL,[FDATE][DATETIME]NOT NULL,[FPURCHASEORGID][INT]NOT NULL,[FENTRYID][INT]NOT NULL,[FMATERIALID][INT]NOT NULL,[FNUMBER][NVARCHAR](80)NOT NULL,[F_XHWT_REPNUMBER][NVARCHAR](150)NOT NULL,[FTaxPrice][DECIMAL](23,10) NULL
) ON [PRIMARY]
GO
插入数据
INSERT [dbo].[MM_T_MATER]([fbillno],[fid],[FCREATEDATE],[FDATE],[FPURCHASEORGID],[FENTRYID],[FMATERIALID],[FNUMBER],[F_XHWT_REPNUMBER],[FTaxPrice]) VALUES (N'INC24010057',113675,CAST(N'2024-01-10T15:30:22.390' AS DateTime),CAST(N'2024-01-10T00:00:00.000' AS DateTime),100006,144354,672265, N'030201064', N'030201064,011401008,011401065,011401066',CAST(5.6000000000ASDecimal(23,10)))
INSERT [dbo].[MM_T_MATER]([fbillno],[fid],[FCREATEDATE],[FDATE],[FPURCHASEORGID],[FENTRYID],[FMATERIALID],[FNUMBER],[F_XHWT_REPNUMBER],[FTaxPrice]) VALUES (N'INC24010057',113675,CAST(N'2024-01-10T15:30:22.390' AS DateTime),CAST(N'2024-01-10T00:00:00.000' AS DateTime),100006,144355,672270, N'030201082', N'030201082,011401008,011401027,011401066,011401084',CAST(5.3000000000ASDecimal(23,10)))
方式一
SELECT a.*,CASE WHEN a.F_XHWT_REPNUMBER='' THEN a.FNUMBER else b.[fvalue] end FNewValue
FROM
(
SELECT *,[fvalue]=CAST('<v>'+REPLACE(F_XHWT_REPNUMBER,',','</v><v>')+'</v>' AS xml)FROM MM_T_MATER
) a
OUTER APPLY (
SELECT [fvalue]=T.C.value('.','varchar(50)') FROM a.[fvalue].nodes('/v')AST(C)) b
方式二
SELECT a.*,CASE WHEN a.F_XHWT_REPNUMBER='' THEN a.FNUMBER else t.fid end FNewValue
FROM MM_T_MATER a
OUTER APPLYfn_StrSplit(a.F_XHWT_REPNUMBER,',') t