TSQL Challenge #14 - Identify the longest sequence of characters in a string
This challenge is all about identifying sequences of characters in a string using a SET based query. The task is to identify the longest sequence of characters within a given string.
Here is the sample data for this challenge
Data
------------------------------------
9992EDC6-D117-4DEE-B410-4E5FAE46AE97
0BFC936B-BD9A-4C6A-AFB2-CF3F1752F8B1
4A73E7EB-7777-4A04-9258-F1E75097977C
5AAF477C-274D-400D-9067-035968F33B19
725DA718-30D0-44A9-B36A-89F27CDFEEDE
8083ED5A-D3B9-4694-BB04-F0B09C588888
Here is the expected output
Data Char Pos Len
------------------------------------ ---- ---- ----
8083ED5A-D3B9-4694-BB04-F0B09C588888 B 20 2
8083ED5A-D3B9-4694-BB04-F0B09C588888 8 32 5
4A73E7EB-7777-4A04-9258-F1E75097977C 7 10 4
4A73E7EB-7777-4A04-9258-F1E75097977C 7 34 2
9992EDC6-D117-4DEE-B410-4E5FAE46AE97 9 1 3
9992EDC6-D117-4DEE-B410-4E5FAE46AE97 1 11 2
9992EDC6-D117-4DEE-B410-4E5FAE46AE97 E 17 2
5AAF477C-274D-400D-9067-035968F33B19 A 2 2
5AAF477C-274D-400D-9067-035968F33B19 7 6 2
5AAF477C-274D-400D-9067-035968F33B19 0 16 2
5AAF477C-274D-400D-9067-035968F33B19 3 32 2
725DA718-30D0-44A9-B36A-89F27CDFEEDE 4 15 2
725DA718-30D0-44A9-B36A-89F27CDFEEDE E 33 2
Column "char" shows the character having longest sequence within the string. "pos" shows the starting position of the sequence and the "len" column shows the length of the sequence.
The output should be ordered as given in the 'expected output' listing. The data having the longest sequence should come on top followed by the next longest sequence (descending order). Within each data, the rows should be ordered by the position at which the sequence starts.
Sample Data
Use the following script to generate the sample data needed for this challenge
DECLARE @t TABLE (Data VARCHAR(40) )
INSERT @t (Data) SELECT '9992EDC6-D117-4DEE-B410-4E5FAE46AE97'
INSERT @t (Data) SELECT '0BFC936B-BD9A-4C6A-AFB2-CF3F1752F8B1'
INSERT @t (Data) SELECT '4A73E7EB-7777-4A04-9258-F1E75097977C'
INSERT @t (Data) SELECT '5AAF477C-274D-400D-9067-035968F33B19'
INSERT @t (Data) SELECT '725DA718-30D0-44A9-B36A-89F27CDFEEDE'
INSERT @t (Data) SELECT '8083ED5A-D3B9-4694-BB04-F0B09C588888'
SELECT * FROM @t
Notes
- Read the Submission Guidelines and make sure that your solution follows them.
- The solution should work on SQL Server 2005, 2008 or later versions
- Use this forum for any questions related to TSQL Challenge #14
About the Authors.