Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract string from a text after a keyword in sql?

Tags:

sql

sqlite

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.

like image 260
zim90 Avatar asked Aug 06 '15 10:08

zim90


People also ask

How do I get the string after a specific character in SQL?

CHARINDEX function in SQL queriesThe CHARINDEX() function returns the substring position inside the specified string. It works reverse to the SUBSTRING function. The substring() returns the string from the starting position however the CHARINDEX returns the substring position.

How do I extract a string from a word in SQL?

The SUBSTRING() function extracts some characters from a string.

How do I select everything after a specific character in SQL?

You can use the MySQL SUBSTRING_INDEX() function to return everything before or after a certain character (or characters) in a string. This function allows you to specify the delimiter to use, and you can specify which one (in the event that there's more than one in the string).


1 Answers

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.

like image 176
psoshmo Avatar answered Sep 25 '22 12:09

psoshmo