-2

我有一列包含以下行

"XX - Make a determination from receipt of request within 15 calendar days"
"Make a determination from receipt of request or necessary info within 2 business days"
"YYYYYYY - Make a determination from receipt of request within 72 calendar hours"
"ZZZZZZZZZZZ - Make a determination from receipt of request within 30 calendar days"

以及具有相似模式的所有类型的不同组合

"XXXXXXXXXXXXXXXXXXXXX"  from    "text I want to get" within ZZZZZZZZZZZZZZZZZZZZ

X 和 Z 的长度会发生变化,from 和 within 之间的文本也会发生变化,但 from 和 inside 始终存在于文本中。如何进行查询以获取要进入表变量的文本,以便将其与同一表中的其他列进行比较,看看它们是否匹配?

我有表变量和标量变量来获取值。我只需要正确的选择语句。

4

1 回答 1

1

这真的很丑。修复表格并停止将不同的数据点存储在您每次都需要像这样解析的字符串中。如果您的主管不明白为什么会这样不好,请询问他是否愿意在每次打开电视时重新编程遥控器。

SET NOCOUNT ON;

DECLARE @t TABLE(c VARCHAR(8000));

INSERT @t VALUES
('XX - Make...ion from receipt of request within 15 calendar days'),
('Make a de...ion from receipt of request or necessary info within 2 business days'),
('YYYYYYY - Make...ion from receipt of request within 72 calendar hours'),
('ZZZZZZZZZZZ - Make...ion from receipt of request within 30 calendar days');

SELECT SUBSTRING(c, 1, CHARINDEX('within', c)-1) FROM
(SELECT c = SUBSTRING(c, CHARINDEX('from', c) + 5, 8000) FROM @t) AS x;

结果:

receipt of request 
receipt of request or necessary info 
receipt of request 
receipt of request 
于 2013-09-12T17:19:10.243 回答