gpt4 book ai didi

sql - 比较sql server中两个字符串中的数字

转载 作者:行者123 更新时间:2023-12-04 20:10:48 25 4
gpt4 key购买 nike

我有两个字符串作为@CountryLocationIDs 和@LocationIDs,其值:

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

然后我需要另一个变量中的输出:

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

任何人都请帮忙...提前致谢...

最佳答案

我创建了接受两个参数的表值函数,第一个是带有 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

创建函数后,只需使用 UNION set operator 这样:

已编辑

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

使用 UNION 你会自动从两个字符串中得到不同的值,所以你不需要使用 DISTINCT 子句

关于sql - 比较sql server中两个字符串中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14641658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com