我在SQL Server中有两个字符串。
例如:
declare @str1 as varchar(max)
declare @str2 as varchar(max)
set @str1 ='10:00am,2:00pm'
set @str2 = '10:00am,12:00pm,2:00pm,4:00pm,6:00pm,8:00pm'
我希望比较两个字符串,并希望从@str2
获取那些不在@str1
中的字符串。
这意味着结果应为:
@str3 = '12:00pm,4:00pm,6:00pm,8:00pm'
答案 0 :(得分:1)
试试这个: -
declare @str1 as varchar(max)
declare @str2 as varchar(max)
set @str1 ='10:00am,2:00pm'
set @str2 = '10:00am,12:00pm,2:00pm,4:00pm,6:00pm,8:00pm'
--the below 2 CTE's are used for splitting the string into different rows
;with cteStr1(str1) as
(
SELECT
RIGHT(LEFT(@str1,Number-1),
CHARINDEX(',',REVERSE(LEFT(','+@str1,Number-1)))) as str1
FROM
master..spt_values
WHERE
Type = 'P' AND Number BETWEEN 1 AND LEN(@str1)+1
AND
(SUBSTRING(@str1,Number,1) = ',' OR SUBSTRING(@str1,Number,1) = '')
),cteStr2(str2) as
(
SELECT
RIGHT(LEFT(@str2,Number-1),
CHARINDEX(',',REVERSE(LEFT(','+@str2,Number-1)))) as str2
FROM
master..spt_values
WHERE
Type = 'P' AND Number BETWEEN 1 AND LEN(@str2)+1
AND
(SUBSTRING(@str2,Number,1) = ',' OR SUBSTRING(@str2,Number,1) = '')
)
Select str2 from cteStr2
except
select str1 from cteStr1
答案 1 :(得分:1)
试试这个
DECLARE @str1 VARCHAR(MAX)
DECLARE @str2 VARCHAR(MAX)
SET @str1 ='10:00am,2:00pm'
SET @str2 = '10:00am,12:00pm,2:00pm,4:00pm,6:00pm,8:00pm'
SET @str1 = ',' + @str1 + ','
SET @str2 = ',' + @str2 + ','
DECLARE @name NVARCHAR(255)
DECLARE @pos INT
WHILE CHARINDEX(',', @str1) > 0
BEGIN
SELECT @pos = CHARINDEX(',', @str1)
SELECT @name = SUBSTRING(@str1, 1, @pos-1)
SELECT @str1 = SUBSTRING(@str1, @pos+1, LEN(@str1)-@pos)
IF @name <> ''
BEGIN
SET @str2 = REPLACE(@str2,','+@name,'')
END
END
SET @str2 = REPLACE(@str2,','+@name,'')
SELECT SUBSTRING(@str2, 2, LEN(@str2)-2) AS Result
答案 2 :(得分:0)
试试这个 -
DECLARE
@str1 VARCHAR(500)
, @str2 VARCHAR(500)
SELECT
@str1 = '10:00am,2:00pm'
, @str2 = '10:00am,12:00pm,2:00pm,4:00pm,6:00pm,8:00pm'
;WITH cte AS
(
SELECT
id = p.value('(./n)[1]', 'INT')
, tm = p.value('(./s)[1]', 'VARCHAR(500)')
FROM (
SELECT field = CAST('<r><s>' + REPLACE(SUBSTRING(t.string + ',', 1, LEN(t.string + ',')), ',', '</s><n>' + CAST(t.id AS VARCHAR(10)) + '</n></r><r><s>') + '</s></r>' AS XML)
FROM (
SELECT string = @str1, id = 1
UNION ALL
SELECT @str2, 2
) t
) d
CROSS APPLY field.nodes('/r') t(p)
WHERE t.p.exist('n') = 1
)
SELECT tm FROM cte WHERE id = 2
EXCEPT
SELECT tm FROM cte WHERE id = 1
或试试这个 -
;WITH cte2 AS
(
SELECT
t.id
, tm =
SUBSTRING(
',' + t.string + ','
, number + 1
, CHARINDEX(',', ',' + t.string + ',', number + 1) - number - 1)
FROM (
SELECT string = @str1, id = 1
UNION ALL
SELECT @str2, 2
) t
CROSS JOIN [master].dbo.spt_values n
WHERE [type] = 'p'
AND number <= LEN(',' + t.string + ',') - 1
AND SUBSTRING(',' + t.string + ',', number, 1) = ','
)
SELECT tm FROM cte2 WHERE id = 2
EXCEPT
SELECT tm FROM cte2 WHERE id = 1