【发布时间】:2013-07-18 06:12:29
【问题描述】:
我有一个用逗号分隔的列值
GoalTag:All Tags,TaskTag:All Tags,GoalId:All,TaskId:All,MaxGoal:5,MaxTask:5
如您所见,我有 6 个用逗号分隔的值,所以当我拆分时,第一个值将是
GoalTag:All Tags
我如何通过调用表值函数来做到这一点(获取由逗号分隔的值)
Select * from dbo.CustomSplit((SELECT FilterNames FROM TblUserFilterView where UserId = 325 AND Entity = 'Dashboard'),',')
dbo.CustomSplit 的定义看起来像
ALTER FUNCTION [dbo].[CustomSplit](@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
现在我需要做的是,我需要获取":" 之后的值,即“所有标签”,它可能是一些其他记录的 id,假设它可能是“142”。我需要获取这个Id,然后从表中获取对应的值。
我该怎么做?
【问题讨论】:
标签: sql sql-server-2008 split