3

我想找出字符串中最长的字母序列

例如在单词Honorificabcdwert中,输出将是abcd.

我该怎么做?

我的想法是获取 Ascii,然后计算序列,直到它在某个点中断。但我只能继续

DECLARE @t TABLE(ID INT IDENTITY,String VARCHAR(100))
INSERT INTO @t SELECT 'Honorificabcdwert'

;with Get_Individual_Chars_Cte AS
( 
   SELECT 
        ID
        ,Row_ID =ROW_NUMBER() Over(PARTITION by ID Order by ID) 
        ,SUBSTRING(String,Number,1) AS [Char]
        ,ASCII(SUBSTRING(String,Number,1)) AS [Ascii Value]

FROM @t  
INNER JOIN master.dbo.spt_values ON
 Number BETWEEN 1 AND LEN(String)
 AND type='P'

)

Select * from Get_Individual_Chars_Cte 

在这之后我不知道该怎么办。为此或任何其他方式所需的帮助。

4

2 回答 2

2

这有帮助吗

DECLARE @t TABLE(ID INT IDENTITY,String VARCHAR(100))
INSERT INTO @t 
SELECT 'Honorificabcdwert' UNION ALL
SELECT 'AbCdEfxy' UNION ALL
SELECT 'abc1234defg' UNION ALL
SELECT 'XYZABCPPCKLMIDBABC' UNION ALL
SELECT 'MNOP$%^&~()MNOPQRS;:'


SELECT ID, OriginalString,Sequence
FROM (SELECT ID, REPLACE(string,'%','') AS Sequence,OriginalString,
      ROW_NUMBER() OVER(PARTITION BY ID ORDER BY LEN(string) DESC, string) AS rn
      FROM (SELECT OriginalString = b.String, CASE WHEN b.String LIKE a.strings THEN a.strings ELSE NULL END AS string, 
            b.ID, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY LEN(strings) DESC, strings) AS rn
            FROM (SELECT COALESCE('%' + b.strings+a.strings + '%','%' + a.strings + '%') AS strings
                  FROM (SELECT SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',t1.N,t2.N-t1.N+1) AS strings, t1.N
                        FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),
                                    (9),(10),(11),(12),(13),(14),(15),
                                    (16),(17),(18),(19),(20),(21),(22),
                                    (23),(24),(25),(26)) t1(N)
                        CROSS JOIN (VALUES(1),(2),(3),(4),(5),(6),(7),(8),
                                          (9),(10),(11),(12),(13),(14),(15),
                                          (16),(17),(18),(19),(20),(21),(22),
                                          (23),(24),(25),(26)) t2(N)
                        WHERE t1.N <= t2.N) a
                  LEFT OUTER JOIN (SELECT REVERSE(SUBSTRING('ZYXWVUTSRQPONMLKJIHGFEDCBA',1,N)) AS strings, 1 AS ID
                                   FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),
                                               (9),(10),(11),(12),(13),(14),(15),
                                               (16),(17),(18),(19),(20),(21),(22),
                                               (23),(24),(25),(26)) t1(N)
                                   UNION ALL SELECT '', 1) b ON a.N = b.ID) a
            CROSS JOIN @t b) a ) a
WHERE a.rn = 1
ORDER BY a.ID

结果

ID  OriginalString          Sequence
1   Honorificabcdwert       ABCD
2   AbCdEfxy                ABCDEF
3   abc1234defg             DEFG
4   XYZABCPPCKLMIDBABC      XYZABC
5   MNOP$%^&~()MNOPQRS;:    MNOPQRS

根据您在与@Martin Smith 讨论过程中提供的意见,该程序正在开发中。请对其进行测试,并让我知道它是否满足您的要求。

于 2012-09-24T04:20:18.157 回答
0

对于字符按字母顺序上升的连续行(此处将字母顺序与 ASCII 顺序等同)ROW_NUMBER() OVER (ORDER BY Row_ID) - [Ascii Value]将是相同的。

这本身是不够的,但是对于ABCZE将放在E同一组中的字符串,ABC那么您需要第二次操作来查找该分组序列中的间隙。

像下面这样的事情应该做到这一点。

DECLARE @t TABLE(ID INT IDENTITY,String VARCHAR(100))
INSERT INTO @t SELECT 'Honorificabcdwfrt'

;with Get_Individual_Chars_Cte AS
( 
   SELECT 
        ID
        ,Row_ID =ROW_NUMBER() Over(PARTITION by ID Order by ID) 
        ,SUBSTRING(String,number,1) AS [Char]
        ,ASCII(SUBSTRING(String,number,1)) AS [Ascii Value]

FROM @t  
INNER JOIN master.dbo.spt_values ON
 number BETWEEN 1 AND LEN(String)
 AND type='P'
)
, T1 AS
(
Select *,
       ROW_NUMBER() OVER (ORDER BY Row_ID) - [Ascii Value] AS RN
 from Get_Individual_Chars_Cte 
 ), T2 AS
 (
 SELECT *,
        ROW_NUMBER() OVER (ORDER BY Row_ID) - 
        ROW_NUMBER() OVER (PARTITION BY RN ORDER BY Row_ID) AS Grp
 FROM T1
 )
 SELECT TOP 1 WITH TIES *
 FROM T2
 ORDER BY  COUNT(*) OVER (PARTITION BY RN, Grp) DESC
于 2012-09-22T10:44:44.233 回答