从不同长度的文本值字段中选择特定文本

时间:2013-09-12 17:07:39

标签: sql-server

我的列中包含以下行

"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和within之间的文本。如何进行查询以获取我想要进入表变量的文本,以便将其与同一表中的其他列进行比较,看看它们是否匹配?

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

1 个答案:

答案 0 :(得分: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