如何在两个单词/字符之间获取文本

时间:2016-08-31 07:11:05

标签: sql-server

如何在两个已知单词之间获取一些文字?

子选择示例:

(SELECT TOP(1)Note
FROM Clients
WHERE (ID=@ID) AND (Note IS NOT NULL) AND (Note NOT LIKE '%Damaged%')) AS Note

返回:

Blablabla bla bla blabla bla bla bla (21.08.2015) "some text" word bla bla
Blablabla bla bla blabla bla bla bla (25.08.2015) "another text" word bla bla 
Blablabla bla bla blabla bla bla bla (28.08.2015) "TEXT I NEED TO GET" word bla bla blabla bla bla.

所以我需要在最后一个“)”和最后一个“单词”之间获取文本

尝试了一些魔法,但它不适用于上一个“)”和最后一个“单词”

4 个答案:

答案 0 :(得分:1)

也许是这样的:

您尝试找到 last occurence 。这是还原字符串中的第一个......

- 这是一个用来保存字符串的表的模型:

DECLARE @tbl TABLE(YourLine VARCHAR(100));
INSERT INTO @tbl VALUES
 ('Blablabla bla bla blabla bla bla bla (21.08.2015) "some text" word bla bla')
,('Blablabla bla bla blabla bla bla bla (25.08.2015) "another text" word bla bla') 
,('Blablabla bla bla blabla bla bla bla (28.08.2015) "TEXT I NEED TO GET" word bla bla blabla bla bla.');

- 在这里我指定您要搜索的单词

DECLARE @SearchWord VARCHAR(100)='word';

- 这是查询

SELECT REVERSE(C.CutOut) AS YourSnippet
FROM @tbl AS tbl

- CROSS APPLY逐行工作:获取当前行并将其还原!

CROSS APPLY(SELECT REVERSE(tbl.YourLine) AS RevLine) AS A

- 找到第一次出现的)以及第一次出现还原的搜索字

CROSS APPLY(SELECT CHARINDEX(')',A.RevLine) AS PosParanthesis
                  ,CHARINDEX(REVERSE(@SearchWord),A.RevLine) AS PosWord) AS B 

- 在找到计算长度的位置剪切字符串

CROSS APPLY(SELECT SUBSTRING(A.RevLine,B.PosWord + LEN(@SearchWord)+1,B.PosParanthesis-B.PosWord-LEN(@SearchWord)-1) AS CutOut) AS C

las CROSS APPLY的结果是你的目标字符串,但相反。因此,主SELECT ...

中的最后一个反转

答案 1 :(得分:0)

declare @start varchar(100)
set @start = 'startword'

declare @end varchar(100)
set @end = 'endword'

SELECT TOP(1) Note , substring( LEFT(note, charindex(@end, note, CHARINDEX(@start, note))-1), CHARINDEX(@start, note) + len(@start), LEN(note))   result
FROM Clients
WHERE (ID=@ID) AND (Note IS NOT NULL) 

答案 2 :(得分:0)

org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 2.0 failed 4 times, most recent failure: Lost task 1.3 in stage 2.0 (TID 38, i89810.sbb.ch): java.io.IOException: org.apache.spark.SparkException: Failed to get broadcast_3_piece0 of broadcast_3
         at org.apache.spark.util.Utils$.tryOrIOException(Utils.scala:1222)
         at org.apache.spark.broadcast.TorrentBroadcast.readBroadcastBlock(TorrentBroadcast.scala:165)
         at org.apache.spark.broadcast.TorrentBroadcast._value$lzycompute(TorrentBroadcast.scala:64)
         at org.apache.spark.broadcast.TorrentBroadcast._value(TorrentBroadcast.scala:64)
         at org.apache.spark.broadcast.TorrentBroadcast.getValue(TorrentBroadcast.scala:88)
         at org.apache.spark.broadcast.Broadcast.value(Broadcast.scala:70)
         at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:62)
         at org.apache.spark.scheduler.Task.run(Task.scala:89)
         at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
         at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.spark.SparkException: Failed to get broadcast_3_piece0 of broadcast_3
         at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$org$apache$spark$broadcast$TorrentBroadcast$$readBlocks$1$$anonfun$2.apply(TorrentBroadcast.scala:138)
         at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$org$apache$spark$broadcast$TorrentBroadcast$$readBlocks$1$$anonfun$2.apply(TorrentBroadcast.scala:138)
         at scala.Option.getOrElse(Option.scala:120)
         at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$org$apache$spark$broadcast$TorrentBroadcast$$readBlocks$1.apply$mcVI$sp(TorrentBroadcast.scala:137)
         at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$org$apache$spark$broadcast$TorrentBroadcast$$readBlocks$1.apply(TorrentBroadcast.scala:120)
         at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$org$apache$spark$broadcast$TorrentBroadcast$$readBlocks$1.apply(TorrentBroadcast.scala:120)
         at scala.collection.immutable.List.foreach(List.scala:318)
         at org.apache.spark.broadcast.TorrentBroadcast.org$apache$spark$broadcast$TorrentBroadcast$$readBlocks(TorrentBroadcast.scala:120)
         at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$readBroadcastBlock$1.apply(TorrentBroadcast.scala:175)
         at org.apache.spark.util.Utils$.tryOrIOException(Utils.scala:1219)
         ... 11 more

答案 3 :(得分:0)

伙计们我找到了自己的解决方案:

  

DECLARE @S VARCHAR(MAX)='Blablabla bla bla blabla bla bla bla   (21.08.2015)“一些文字”一词​​bla bla Blablabla bla bla blabla bla bla   bla(25.08.2015)“另一个文字”一词​​bla bla blablabla bla bla blabla   bla bla bla(28.08.2015)“文字我需要得到”字bla bla blabla bla   BLA'。

     

SELECT LEFT(REVERSE(LEFT(REVERSE(@S),CHARINDEX(')',REVERSE(@S)) -   2))+'word',CHARINDEX('word',REVERSE(LEFT(REVERSE(@S),CHARINDEX(')',   REVERSE(@S)) - 2))+'word') - 1)AS回复

完全返回:

  

“我需要获取的文字”

谢谢大家带领我到这一点。