gpt4 book ai didi

sql - 如何使用 MS SQL 获取所有字段记录中使用的不同单词列表?

转载 作者:行者123 更新时间:2023-12-05 01:34:49 27 4
gpt4 key购买 nike

如果我有一个名为“description”的表字段,那么获取该字段中使用的所有不同单词的记录列表的 SQL(使用 MS SQL)是什么。

例如:

如果表格的“描述”字段包含以下内容:

Record1 "The dog jumped over the fence."
Record2 "The giant tripped on the fence."
...

SQL 记录输出将是:
"The","giant","dog","jumped","tripped","on","over","fence"

最佳答案

我不认为你可以用 SELECT 来做到这一点。最好的机会是编写一个用户定义的函数,该函数返回一个包含所有单词的表,然后对其执行 SELECT DISTINCT。

免责声明:功能 dbo.Split 来自 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

CREATE TABLE test
(
id int identity(1, 1) not null,
description varchar(50) not null
)

INSERT INTO test VALUES('The dog jumped over the fence')
INSERT INTO test VALUES('The giant tripped on the fence')

CREATE FUNCTION dbo.Split
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1

While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End

Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))

Return
END

CREATE FUNCTION dbo.SplitAll(@SplitOn nvarchar(5))
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
DECLARE My_Cursor CURSOR FOR SELECT Description FROM dbo.test
DECLARE @description varchar(50)

OPEN My_Cursor
FETCH NEXT FROM My_Cursor INTO @description
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @RtnValue
SELECT Data FROM dbo.Split(@description, @SplitOn)
FETCH NEXT FROM My_Cursor INTO @description
END
CLOSE My_Cursor
DEALLOCATE My_Cursor

RETURN

END

SELECT DISTINCT Data FROM dbo.SplitAll(N' ')

关于sql - 如何使用 MS SQL 获取所有字段记录中使用的不同单词列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/106275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com