【发布时间】:2009-08-13 17:49:13
【问题描述】:
这里是一个split函数,它可以应用为dbo.Split('sf,we,fs,we',','),当我将字符串更改为列名时它不起作用,例如dbo.Split(table.columnName,',')。
Select * from dbo.Split('dirk@test.com','@')
有效,但是
Select * from dbo.Split((Select Email from Users),'@')
有错误信息:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','
函数在这里:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
引用:SQL 2005 Split Comma Separated Column on Delimiter
或者有人可以给我一个类似的功能,可以将一列分成两列
【问题讨论】:
-
您能否详细说明“它不起作用”部分?
-
该函数对 varchar 值进行操作,而不是对列进行操作。当然,该值可能来自列。您正在使用它来将 CSV 数据标准化为从一开始就应该存储的多个列,对吧?