3

I am having two strings as @CountryLocationIDs and @LocationIDs with values:

@CountryLocationIDs = 400,600,150,850,160,250
@LocationIDs1       = 600,150,900

Then I need the output in another variable as:

@LocationIDs = 400,600,150,850,160,250,900

Anybody please help out... Thanks in advance...

1
  • 4
    It looks as if you are paying the price for denormalisation. Commented Feb 1, 2013 at 7:38

3 Answers 3

3

I have created table-valued function which accepts two parameters, first is string with IDs, and second is delimiter in string.

CREATE FUNCTION [dbo].[Split](@String nvarchar(4000), @Delimiter char(1))       
returns @temptable TABLE (items nvarchar(4000))       
as       
begin       
    declare @idx int       
    declare @slice nvarchar(4000)       

    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  

After creating function, just use UNION set operator on this way:

EDITED

WITH ListCTE AS 
(
select items from dbo.split('400,600,150,850,160,250', ',')
union
select items from dbo.split('600,150,900', ',')
)
SELECT TOP 1

   MemberList = substring((SELECT ( ', ' + items )
                           FROM ListCTE t2
                           ORDER BY 
                              items
                           FOR XML PATH( '' )
                          ), 3, 1000 )FROM ListCTE t1

With UNION you will automatically get distinct values from both strings, so you don't need to use DISTINCT clause

Sign up to request clarification or add additional context in comments.

2 Comments

I need to save tis output in a varchar variable... While trying to store, I am getting the error as : Msg 512, Level 16, State 1, Procedure spGetDLMembersByListType1, Line 51 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
SET @Location_IDs = ((select * from dbo.SplitString(@CountryLocation_IDs, ',')) union (select * from dbo.SplitString(@Location_IDs, ',')))
3

Also you can use option with dynamic management function sys.dm_fts_parser
Before script execution you need check full-text component is installed:

SELECT FULLTEXTSERVICEPROPERTY ('IsFulltextInstalled')

0 = Full-text is not installed. 1 = Full-text is installed. NULL = Invalid input, or error.

If 0 = Full-text is not installed then this post is necessary to you How to install fulltext on sql server 2008?

DECLARE @CountryLocationIDs nvarchar(100) = '400,600,150,850,160,250',
        @LocationIDs1       nvarchar(100) = '600,150,900',
        @LocationIDs        nvarchar(100) = N''

SELECT @LocationIDs += display_term + ','
FROM sys.dm_fts_parser('"'+ 'nn,' + @CountryLocationIDs + ',' + @LocationIDs1 + '"', 1033, NULL, 0)  
WHERE display_term NOT LIKE 'nn%'
GROUP BY display_term

SELECT LEFT(@LocationIDs, LEN(@LocationIDs) - 1)

Comments

0

This will help you through programming

and you may also refer this

1 Comment

Link only answers are frowned upon here. What if the page(s) you've linked to are moved or deleted? It would be a lot more helpful to include a summary to what you're linking to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.