sql - Getting id values from a column and then get value from another table -
i have column value separated comma as
goaltag:all tags,tasktag:all tags,goalid:all,taskid:all,maxgoal:5,maxtask:5
as can see have 6 values separated comma, when split first value be
goaltag:all tags
how (get values seperated comma is) calling table valued function
select * dbo.customsplit((select filternames tbluserfilterview userid = 325 , entity = 'dashboard'),',')
the definition dbo.customsplit
looks like
alter function [dbo].[customsplit](@string varchar(8000), @delimiter char(1)) returns @temptable table (items varchar(8000)) begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@string)<1 or @string 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 @temptable(items) values(@slice) set @string = right(@string,len(@string) - @idx) if len(@string) = 0 break end return end
now need is, need value after ":"
i.e. "all tags" may id other records let's may "142". need id , corresponding value table.
how can this?
try this:
select substring(s.items, 1 + charindex ( ':', s.items), len(s.items) - charindex (':', s.items)) (select * dbo.customsplit((select filternames tbluserfilterview userid = 325 , entity = 'dashboard'), ',')) s
you may create function:
create function [dbo].[customsplit2](@string varchar(8000), @delimiter char(1)) returns varchar(4000) begin declare @result nvarchar(4000) select @result = substring(@string, 1 + charindex ( @delimiter, @string), len(@string) - charindex (@delimiter, @string) ) return @result end
and use like:
select [dbo].customsplit2(s.items, ':') tag (select * dbo.customsplit((select filternames tbluserfilterview userid = 325 , entity = 'dashboard'), ',')) s
Comments
Post a Comment