Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving first X words from a string in Oracle Select

I need to select the first X words in a string, where x can be any number from 0-100. Is there an easy way to do this? I found the following example to select the first 2 words from a string:

select regexp_replace('Hello world this is a test', '(\w+ \w+).*$','\1') as first_two
from dual

How would I select the first X words from a string where X can be a number from 0-100?

like image 469
Robert Smith Avatar asked Jun 03 '15 12:06

Robert Smith


People also ask

How do you extract a particular word from a string in Oracle?

SELECT REGEXP_SUBSTR ( 'select * from TEMP_TABLE where trunc(xyz_xyz) = ''xyz'' and trunc(abc_abc) = ''abc'')' , ' trunc[^'']''([^'']+)''' , 1 , n , 'i' , 1 ) FROM dual ; returns the n-th such substring.

How extract part of a string in Oracle?

Use a SUBSTR() function. The first argument is the string or the column name. The second argument is the index of the character at which the substring should begin. The third argument is the length of the substring.

What is substr () Instr () functions?

The substr functions allows you to extract a substring from a string. The instr function returns the location of a substring in a string.

How do I cut a string after a specific character in Oracle?

Best Answer. We can use SUBSTR to get a sub-string when we know the starting point and number of characters. In this case, the starting point is 1, and we can use INSTR to find where the slash is, and get that many characters minus 1.


2 Answers

Selecting the first four words:

select
   regexp_replace(
     'Hello world this is a test etc',
     '(((\w+)\s){4}).*',  -- Change 4 to wanted number of words here!
     '\1'
   )
   from dual;

Edit

The above solution only works if the words are seperated by exactly one white space character. If the words are seperated by one or more white space characters, the \s must be extended to \s+:

select
   regexp_replace(
     'Hello    world   this   is a   test     etc',
     '(((\w+)\s+){4}).*',  -- Change 4 to wanted number of words here!
     '\1'
   )
   from dual;
like image 145
René Nyffenegger Avatar answered Oct 29 '22 16:10

René Nyffenegger


This method takes the result of extracting the number of words you want, then reduces multiple spaces to one:

select trim(regexp_replace(regexp_substr('Hello         world this is a test etc', '(([^ ]*)( |$)*){3}'), ' +', ' '))
from dual;

EDIT: This is getting ugly, but wrapped a TRIM() around it to get rid of the trailing space (the one after the last word selected).

like image 24
Gary_W Avatar answered Oct 29 '22 16:10

Gary_W