简体   繁体   English

Substring SQL 服务器中带有 charindex 的字符串中的一个单词

[英]Substring a word from string with charindex in SQL server

I am trying to write a query to extract a word from a string.我正在尝试编写一个查询以从字符串中提取一个单词。 For example, I want to get the word Final from the sentence: this is the final exam .例如,我想从句子中得到Final这个词: this is the final exam

I want only to get the word 'final' using charindex to determine the start and end of the substring function.我只想使用 charindex 获得“final”这个词来确定 substring function 的开始和结束。

I think this is what you are asking for.我想这就是你所要求的。 Please have a look at this:请看一下:

Declare @start INT;
Declare @end int;

set @start=CHARINDEX('Fi','this is the final exam');
set @end =CHARINDEX('al','this is the final exam');
if(@start >0 and @end>0)
Begin
SELECT
       SUBSTRING('this is the final exam', 
                  @start, 
                  5)
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM