📜  在 sql server 中用逗号分割字符串 - SQL 代码示例

📅  最后修改于: 2022-03-11 15:05:36.180000             🧑  作者: Mango

代码示例1
CREATE FUNCTION Split
(
  @delimited nvarchar(max),
  @delimiter nvarchar(100)
) RETURNS @t TABLE
(
-- Id column can be commented out, not required for sql splitting string
  id int identity(1,1), -- I use this column for numbering splitted parts
  val nvarchar(max)
)
AS
BEGIN
  declare @xml xml
  set @xml = N'' + replace(@delimited,@delimiter,'') + ''

  insert into @t(val)
  select
    r.value('.','varchar(max)') as item
  from @xml.nodes('//root/r') as records(r)

  RETURN
END
GO