使用SQL查找字符串中最长的单词

时间:2017-04-19 17:18:44

标签: sql sql-server

我在网上找到了以下代码段:

DECLARE @str VARCHAR(5000)= 'aaaa bbbbb cccccccc ddddddddddddddddddd'

SELECT TOP 1 Split.a.value('.', 'VARCHAR(100)') as longest_Word
FROM   (SELECT Cast ('<M>' + Replace(@str, ' ', '</M><M>') + '</M>' AS XML) AS Data) AS A
       CROSS APPLY Data.nodes ('/M') AS Split(a)
ORDER  BY Len(Split.a.value('.', 'VARCHAR(100)')) DESC 

找到文本文件中最长的单词(在本例中为“dddddddddddddddddddddddddddddddddddddddddddddddddddddddd但是,我不知道如何在每一行中应用此行,以便在每行旁边添加一列,告诉我SQL Server中最长的单词是什么。

SQL Server中需要搜索的每一列应该如下所示,并在其旁边添加了额外的列:

COLUMN 1     RESULT
ABC ABCD     ABCD
BC  BCDE     BCDE
9II IIIIV    IIIIV
...          ...

1 个答案:

答案 0 :(得分:2)

简单的 CROSS APPLY 可以提供帮助。

注意,我使用自己的XML Parser,因为它是“XML Safe”

Declare @YourTable table ([COLUMN 1] varchar(max))
Insert Into @YourTable values
('ABC ABCD'),
('BC  BCDE'),
('9II IIIIV')

Select A.[COLUMN 1]
      ,Result = B.RetVal
 From  @YourTable A
 Cross Apply (
                Select Top 1 *
                 From  (
                        Select RetSeq = Row_Number() over (Order By (Select null))
                              ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace((Select replace(A.[COLUMN 1],' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                        Cross Apply x.nodes('x') AS B(i)
                       ) B1
                 Order by Len(RetVal) Desc,RetSeq
             ) B

返回

COLUMN 1    Result
ABC ABCD    ABCD
BC  BCDE    BCDE
9II IIIIV   IIIIV
  

编辑 - 您想要显示TIES

Declare @YourTable table ([COLUMN 1] varchar(max))
Insert Into @YourTable values
('ABC ABCD'),
('BC  BCDE 1234'),
('9II IIIIV')

Select A.[COLUMN 1]
      ,Result = B.RetVal
 From  @YourTable A
 Cross Apply (
                Select Top 1 with ties *
                 From  (
                        Select RetSeq = Row_Number() over (Order By (Select null))
                              ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace((Select replace(A.[COLUMN 1],' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                        Cross Apply x.nodes('x') AS B(i)
                       ) B1
                 Order by Dense_Rank() over (Order by Len(RetVal) Desc)
             ) B

返回

COLUMN 1        Result
ABC ABCD        ABCD
BC  BCDE 1234   BCDE   --<< Tie 
BC  BCDE 1234   1234   --<< Tie Added for Illustration
9II IIIIV       IIIIV