Extract string from a text after a keyword in sql?

邮差的信 提交于 2020-06-25 21:06:35

问题


I have the necessity to extract content from a text in a SQL field after a keyword. For example if i have a field called description in a table, and the table content for that field is:

Description

asdasf keyword dog

aeee keyword cat

ffffaa keyword wolf

I want to extract and save the text after "keyword " (in this case dog,cat and wolf) and save it in a view or simply show it with a select. Thank you.


回答1:


Here is an example using SUBSTRING():

SELECT SUBSTRING(YourField, CHARINDEX(Keyword,YourField) + LEN(Keyword), LEN(YourField))

Another example:

declare @YourField varchar(200) = 'Mary had a little lamb'
declare @Keyword varchar(200) = 'had'
select SUBSTRING(@YourField,charindex(@Keyword,@YourField) + LEN(@Keyword), LEN(@YourField) )

Result:

 a little lamb

Please note that there is a space before the 'a' in this string.



来源:https://stackoverflow.com/questions/31853466/extract-string-from-a-text-after-a-keyword-in-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!