tsql - Filter an ID Column against a range of values -
i have following sql:
select ',' + ltrim(rtrim(cast(vessel_is_id char(2)))) + ',' 'id' vessels ',' + ltrim(rtrim(cast(vessel_is_id varchar(2)))) + ',' in (',1,2,3,4,5,6,') basically, want filter vessel_is_id against variable list of integer values (which passed in varchar stored proc). now, above sql not work. have rows in table `vessel__is_id' of 1, not returned.
can suggest better approach me? or, if above ok
edit:
sample data
| vessel_is_id | | ------------ | | 1 | | 2 | | 5 | | 3 | | 1 | | 1 | so want returned of above vessel_is_id in variable filter i.e. '1,3' - should return 4 records.
cheers. jas.
if object_id(n'dbo.fn_arraytotable',n'fn') not null drop function [dbo].[fn_arraytotable] go create function [dbo].fn_arraytotable (@array varchar(max)) -- ============================================= -- author: dan andrews -- create date: 04/11/11 -- description: string tabled-valued function -- -- ============================================= returns @output table (data varchar(256)) begin declare @pointer int set @pointer = charindex(',', @array) while @pointer != 0 begin insert @output select rtrim(ltrim(left(@array,@pointer-1))) select @array = right(@array, len(@array)-@pointer), @pointer = charindex(',', @array) end return end which may apply like:
select * dbo.fn_arraytotable('2,3,4,5,2,2') and in case:
select ltrim(rtrim(cast(vessel_is_id char(2)))) 'id' vessels ltrim(rtrim(cast(vessel_is_id varchar(2)))) in (select data dbo.fn_arraytotable('1,2,3,4,5,6')
Comments
Post a Comment