我想删除给定表格列中的重复字符串。
以下是一些例子:
Input | Expected Output
---------------------------
XYXY | XY
AA | A
XYZXYZ | XYZ
ABCABCABC | ABC
我该怎么做?
答案 0 :(得分:5)
此查询对您有所帮助。
SELECT dbo.RemoveDuplicate(ColumnName,VariableLength)FROM TableName。
示例: SELECT dbo.RemoveDuplicate(StudentName,20)FROM Students。
删除重复字符串的功能:
CREATE FUNCTION RemoveDuplicate (@sInputString AS VARCHAR(10), @nLength AS INT)
RETURNS VARCHAR(Max) AS
BEGIN
DECLARE @count INT
DECLARE @new_string VARCHAR(Max)
SET @count=1
WHILE ( @count <= @nLength )
BEGIN
IF ( @new_string IS NULL )
BEGIN
SET @new_string=''
END
SET @new_string=@new_string + Substring(@sInputString, 1, 1)
SET @sInputString=REPLACE(@sInputString, Substring(@sInputString, 1, 1), '')
SET @count=@count + 1
END
RETURN @new_string
END
答案 1 :(得分:1)
使用三个逻辑来获得输出。
首先使用CTE
第二次对于CTE中的每一行都有row_number()
,将在下一步中使用。
第三次是concatenate the rows using group by row_number()
,这是在第二步中创建的。
CREATE TABLE #input
(name VARCHAR(50))
INSERT INTO #input
VALUES ('XYXY'),
('AA'),
('XYZXYZ'),
('ABCABCABC');
WITH cte
AS (SELECT Row_number()OVER (ORDER BY name) rn,
Substring(name, 1, 1) AS sub,
1 AS IDX,
name
FROM #input
WHERE Len(name) > 0
UNION ALL
SELECT rn,Substring(name, IDX + 1, 1) AS sub,
IDX + 1 AS IDX,
name
FROM cte
WHERE IDX < Len(name))
SELECT name INPUT, (SELECT DISTINCT CONVERT(VARCHAR(100), sub)
FROM cte b
WHERE b.rn = a.rn
FOR XML PATH('')) EXPECTED_OUTPUT
FROM cte a
GROUP BY rn ,name
<强>输出强>
INPUT EXPECTED_OUTPUT
--------- ---------------
AA A
ABCABCABC ABC
XYXY XY
XYZXYZ XYZ