In SQL how to find two words after a searched word in string

时间:2018-09-18 20:14:57

标签: sql postgresql

I'm trying to do a SQL query on a table with strings that does the following:

  1. I'm trying to find all strings where the word "poor" is present in a table.
  2. In those strings, I need to identify the word that is two places to the right and copy that to a new column

Table I'm searching

Table that adds column and shows word 2 places to the right

1 个答案:

答案 0 :(得分:0)

You did not provide code so I will not either, instead I will help guide you in the right direction.

Firstly, you will want to find anything with the word "poor" using a wildcard, allowing it to be at the beginning, middle, or end of the string. Next you'll have to find the location of the word "poor" in the string. Finally, do something along the lines of locating the second space to the right of the end of the word "poor", then get anything after that space until the next space because this will contain the word you're looking for (if your strings follow traditional sentence structure).

You'll need to consider what to do if the word "poor" is one of the last words in the string, you may not have another word to look for.