我有大量的描述,每个描述可以是 5 到 20 个句子。我正在尝试将一个脚本放在一起,该脚本将定位并删除一个包含在其前后带有数字的单词的句子。
前面的例子:你好世界。今天的部门有345名员工。祝你有美好的一天。在示例之后:你好世界。祝你有美好的一天。
我现在的主要问题是识别违规行为。
这里的“345 名员工”是导致该句子被删除的原因。但是,每个描述都有不同的数字,并且可能有不同的员工一词变体。我想避免必须创建一个包含所有不同员工变化的表格。
JTB
我有大量的描述,每个描述可以是 5 到 20 个句子。我正在尝试将一个脚本放在一起,该脚本将定位并删除一个包含在其前后带有数字的单词的句子。
前面的例子:你好世界。今天的部门有345名员工。祝你有美好的一天。在示例之后:你好世界。祝你有美好的一天。
我现在的主要问题是识别违规行为。
这里的“345 名员工”是导致该句子被删除的原因。但是,每个描述都有不同的数字,并且可能有不同的员工一词变体。我想避免必须创建一个包含所有不同员工变化的表格。
JTB
这将是一个很好的 SQL 谜题。
免责声明:可能有大量的边缘情况会炸毁它
这将获取一个字符串,将其拆分为一个表,每个句子都有一行,然后删除与条件匹配的行,最后将它们全部连接成一个字符串。
CREATE FUNCTION dbo.fn_SplitRemoveJoin(@Val VARCHAR(2000), @FilterCond VARCHAR(100))
RETURNS VARCHAR(2000)
AS
BEGIN
DECLARE @tbl TABLE (rid INT IDENTITY(1,1), val VARCHAR(2000))
DECLARE @t VARCHAR(2000)
-- Split into table @tbl
WHILE CHARINDEX('.',@Val) > 0
BEGIN
SET @t = LEFT(@Val, CHARINDEX('.', @Val))
INSERT @tbl (val) VALUES (@t)
SET @Val = RIGHT(@Val, LEN(@Val) - LEN(@t))
END
IF (LEN(@Val) > 0)
INSERT @tbl VALUES (@Val)
-- Filter out condition
DELETE FROM @tbl WHERE val LIKE @FilterCond
-- Join back into 1 string
DECLARE @i INT, @rv VARCHAR(2000)
SET @i = 1
WHILE @i <= (SELECT MAX(rid) FROM @tbl)
BEGIN
SELECT @rv = IsNull(@rv,'') + IsNull(val,'') FROM @tbl WHERE rid = @i
SET @i = @i + 1
END
RETURN @rv
END
go
CREATE TABLE #TMP (rid INT IDENTITY(1,1), sentence VARCHAR(2000))
INSERT #tmp (sentence) VALUES ('Hello world. Todays department has 345 employees. Have a good day.')
INSERT #tmp (sentence) VALUES ('Hello world. Todays department has 15 emps. Have a good day. Oh and by the way there are 12 employees somewhere else')
SELECT
rid, sentence, dbo.fn_SplitRemoveJoin(sentence, '%[0-9] Emp%')
FROM #tmp t
返回
rid | sentence | |
1 | Hello world. Todays department has 345 employees. Have a good day. | Hello world. Have a good day.|
2 | Hello world. Todays department has 15 emps. Have a good day. Oh and by the way there are 12 employees somewhere else | Hello world. Have a good day. |
我也使用了拆分/删除/加入技术。
要点是:
.
或!
或?
这是SqlFiddle 演示和代码:
-- Split descriptions into sentences (could use period, exclamation point, or question mark)
-- Delete any sentences that, without whitespace, are like '%[0-9]employ%'
-- Join sentences back into descriptions
;with Splitter as (
select ID
, ltrim(rtrim(Data)) as Data
, cast(null as varchar(max)) as Sentence
, 0 as SentenceNumber
from Descriptions -- Your table here
union all
select ID
, case when Data like '%[.!?]%' then right(Data, len(Data) - patindex('%[.!?]%', Data)) else null end
, case when Data like '%[.!?]%' then left(Data, patindex('%[.!?]%', Data)) else Data end
, SentenceNumber + 1
from Splitter
where Data is not null
), Joiner as (
select ID
, cast('' as varchar(max)) as Data
, 0 as SentenceNumber
from Splitter
group by ID
union all
select j.ID
, j.Data +
-- Don't want "digit+employ" sentences, remove whitespace to search
case when replace(replace(replace(replace(s.Sentence, char(9), ''), char(10), ''), char(13), ''), char(32), '') like '%[0-9]employ%' then '' else s.Sentence end
, s.SentenceNumber
from Joiner j
join Splitter s on j.ID = s.ID and s.SentenceNumber = j.SentenceNumber + 1
)
-- Final Select
select a.ID, a.Data
from Joiner a
join (
-- Only get max SentenceNumber
select ID, max(SentenceNumber) as SentenceNumber
from Joiner
group by ID
) b on a.ID = b.ID and a.SentenceNumber = b.SentenceNumber
order by a.ID, a.SentenceNumber
一种方法来做到这一点。请注意,仅当您在所有句子中都有一个数字时,它才有效。
declare @d VARCHAR(1000) = 'Hello world. Todays department has 345 employees. Have a good day.'
declare @dr VARCHAR(1000)
set @dr = REVERSE(@d)
SELECT REVERSE(RIGHT(@dr,LEN(@dr) - CHARINDEX('.',@dr,PATINDEX('%[0-9]%',@dr))))
+ RIGHT(@d,LEN(@d) - CHARINDEX('.',@d,PATINDEX('%[0-9]%',@d)) + 1)