Remove Duplicate Chars From String
Remove
Duplicate Chars From String
CREATE
FUNCTION
dbo.REMOVE_DUPLICATE_INSTR
(
@datalen_tocheck
INT
,
@string
VARCHAR
(
255
))
RETURNS VARCHAR
(
255
)
AS
BEGIN
DECLARE
@str
VARCHAR
(
255
)
DECLARE
@count
INT
DECLARE
@start
INT
DECLARE
@result
VARCHAR
(
255
)
DECLARE
@end
INT
SET
@start
=
1
SET
@end
=
@datalen_tocheck
SET
@count
=
@datalen_tocheck
SET
@str
=
@string
WHILE
(
@count
<=
255
)
BEGIN
IF
(
@result
IS
NULL)
BEGIN
SET
@result
=
''
END
SET
@result
=
@result
+
SUBSTRING
(
@str
,
@start
,
@end
)
SET
@str
=
REPLACE
(
@str
,
SUBSTRING
(
@str
,
@start
,
@end
),
''
)
SET
@count
=
@count
+
@datalen_tocheck
END
RETURN
@result
END
GO
SET
CONCAT_NULL_YIELDS_NULL
OFF
SELECT dbo.Remove_duplicate_instr(<CHAR lengthOF a duplicate SUBSTRING >,<string contain duplicate>)
Example:
To keep char set in a string unique and remove duplicate 3 char long string run this UDF as inline function.
To keep char set in a string unique and remove duplicate 3 char long string run this UDF as inline function.
SET
CONCAT_NULL_YIELDS_NULL
OFF
SELECT dbo.Remove_duplicate_instr(3,123456789123456456)
Resultset:
123456789
Comments