I am having two strings as @CountryLocationIDs and @LocationIDs with values:
我有两个字符串@ countrylocationid和@ locationid,值为:
@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...
有人请帮忙…提前谢谢…
3 个解决方案
#1
3
I have created table-valued function which accepts two parameters, first is string with IDs, and second is delimiter in string.
我已经创建了表值函数,它接受两个参数,第一个是带id的字符串,第二个是字符串分隔符。
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:
创建函数后,使用UNION set操作符:
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
使用UNION,您将自动从两个字符串中获得不同的值,因此不需要使用不同的子句
#2
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.
0 =未安装全文。1 =全文安装。NULL =无效输入或错误。
If 0 = Full-text is not installed then this post is necessary to you How to install fulltext on sql server 2008?
如果0 =全文本没有安装,那么这篇文章对于您如何在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)
#1
3
I have created table-valued function which accepts two parameters, first is string with IDs, and second is delimiter in string.
我已经创建了表值函数,它接受两个参数,第一个是带id的字符串,第二个是字符串分隔符。
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:
创建函数后,使用UNION set操作符:
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
使用UNION,您将自动从两个字符串中获得不同的值,因此不需要使用不同的子句
#2
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.
0 =未安装全文。1 =全文安装。NULL =无效输入或错误。
If 0 = Full-text is not installed then this post is necessary to you How to install fulltext on sql server 2008?
如果0 =全文本没有安装,那么这篇文章对于您如何在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)