如何选择关键字前后的单词

时间:2019-07-02 12:51:13

标签: sql r sql-server

我有一个名为BomNarration的表,该表的名为Narration的列包含一行文本,并且每一行中都有一个名为“ Rev”的关键字。我想选择关键字前后的单词。一个问题是,在不同的行中,我要选择的单词的长度不同,关键字可以在任何地方。该列中文本行的两个示例是“对这种GX-77568 REV A执行此操作”。和“为此,请遵循BW-8452-355 REV D”。

我正在R Studio上编写sql查询,以发送到sqlserver。现在,我只能抓取关键字前的11个字符以及关键字后的所有字符。

SELECT LEFT(SUBSTRING(BomNarration.Narration, 
    (CHARINDEX('REV', BomNarration.Narration) - 12), 
    LEN(BomNarration.Narration)), 11) AS Drawing,
    LEFT(SUBSTRING(BomNarration.Narration, 
    (CHARINDEX('REV', BomNarration.Narration) + 3), 
    LEN(BomNarration.Narration)), 3) AS RevNum
FROM BomNarration

我希望只显示关键字前的11个字符和关键字后的3个字符。我只想要出现在关键字前后的单词(“ GX-77568”,“ A。”和“ BW-8452-355”“ D”)。但是我得到的只是关键字之前的11个字符以及关键字之后的所有字符(“ ch GX-77568”“ A。”和“ BW-8452-355”“ D为此,这就是那个”)

屏幕上的结果表

   Op Ln     Drawing                        RevNum
<       
1   1 10 ING 33B8222                             -
<       
2   1 13 RSC16A0019   D. AN NCR MUST BE RAISED AND
<       
3   1 20 NG C16A0019                             E
<       
4   5  2 SW-900-0009                             B
<       
5   6  6 SW-800-0004                             A
<       
6  14 24 SW-900-0078                             A
<       
7  15 12 SW-900-0078                             A
<       
8  16  4 SW-800-0006                             A
<       
9  22  6 SW-800-0006                             A
<       
10 25  4 SW-900-0008                             C
<       
11 27  3 SW-900-0055                             B
<       
12 28  4 SW-900-0055                             B
<       
13 29  3 SW-900-0055                             B
<       
14 30  3 SW-900-0055                             B
<       
15 36  1 SW-900-0255                             A
<       
16 36  4 SW-900-0266                             A
<       
17 36  8 SW-900-0057                  A. IF THIS *
<       
18 39  2 SW-900-0041                             A
<       
19 45  3 SW-900-0020                             A
<       
20 46  2 SW-900-0020                             A

请清楚,我将显示整个代码

SELECT BomOperation.Op, 
CASE WHEN BomOperations.AutoNarrCode = 0
THEN BomOperationsStd.Ln
ELSE BomOperations.Ln
END,
CASE WHEN BomOperations.AutoNarrCode = 0
THEN LEFT(SUBSTRING(BomNarrationStd.Narration, (CHARINDEX('REV', BomNarrationStd.Narration) - 12), LEN(BomNarrationStd.Narration)), 11)
ELSE LEFT(SUBSTRING(BomNarration.Narration, (CHARINDEX('REV', BomNarration.Narration) - 12), LEN(BomNarration.Narration)), 11)
END
CASE WHEN BomOperations.AutoNarrCode = 0
THEN LEFT(SUBSTRING(BomNarration.NarrationStd, CHARINDEX('REV', BomNarrationStd.Narration) + 3, LEN(BomNarrationStd.Narration)), 3)
ELSE LEFT(SUBSTRING(BomNarration.Narration, CHARINDEX('REV', BomNarration.Narration) + 3, LEN(BomNarration.Narration)), 3)
END
FROM ......

1 个答案:

答案 0 :(得分:0)

'REV'之后的三个字符

actionsForm.dgActions.DataSource = dgClients.CurrentRow.Cells["Actions"];

演示

Left(SUBSTRING(BomNarration.Narration, CHARINDEX('REV', BomNarration.Narration) + 3, LEN(BomNarration.Narration)), 3)

返回

SELECT LEFT(SUBSTRING(BomNarration.Narration, 
    (CHARINDEX('REV', BomNarration.Narration) - 12), 
    LEN(BomNarration.Narration)), 11) AS Drawing,
    LEFT(SUBSTRING(BomNarration.Narration, 
    (CHARINDEX('REV', BomNarration.Narration) + 3), 
    LEN(BomNarration.Narration)), 3) AS RevNum
FROM (values
   ('do this that for such such GX-77568 REV A.')
   ,('Follow BW-8452-355 REV D for this this that that'))
BomNarration(Narration)

您上次编辑后的代码可以正常工作。