Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL: Return first line of string using regexp_substr

I am trying to return the first line of text from a text box in an SQL query (oracle 11). The content of the text box looks like this:

   X WITHDRAWN

   Explanation.

I want to return the top line, i.e. the X WITHDRAWN. I'm not sure if I can specify to look at the first line only, or to just return all text before a carriage return - either would work.

I think I need to use regexp_substr but I'm not quite sure on the syntax. I have tried:

   regexp_substr(TABLE.TEXT,'^.*$')

but it didn't work, so any assistance would be much appreciated!

EDIT: The solution used:

   select regexp_substr(TABLE.TEXT, '[^,]+['||CHR(10)||']') from tab

EDIT: I noticed I was getting a mixture of line feed and carriage returns returned in my answer, so I've use the following solution to return just the text and no additional characters.

    select 
     replace(replace(regexp_substr(TABLE.TEXT, '[^,]+['||CHR(10)||']'),CHR(10),''),CHR(13),'') 
     from tab 

EDIT: Following @Ben's answer, I've amended my solution to the following:

select
initcap(replace(regexp_substr(TABLE.TEXT, '.*$', 1, 1, 'm'),CHR(13),''))
from tab
like image 295
bawpie Avatar asked Jul 15 '13 11:07

bawpie


People also ask

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.

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.

What is the use of REGEXP_SUBSTR in Oracle?

REGEXP_SUBSTR extends the functionality of the SUBSTR function by letting you search a string for a regular expression pattern. It is also similar to REGEXP_INSTR , but instead of returning the position of the substring, it returns the substring itself.

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.


1 Answers

Parado's regular expression matches everything that's not a comma multiple times followed by a carriage return. This means it won't work for a line-feed or if there's a comma in the text.

Oracle supports multi-line expressions using the m match parameter. When using this mode, $ matches the end of each line as well as the end of the string. You can use this to simply the expression massively to:

regexp_substr(str, '.*$', 1, 1, 'm')

That is match the first occurrence (the first line) of the string that matches anything, followed by the end of the string, counting from the first character.

As an example:

with strings as ( 
 select 'hi
         hi again' as str
   from dual
  union all
 select 'bye
         and again'
   from dual
        )
 select regexp_substr(str, '.*$', 1, 1, 'm')
   from strings
like image 111
Ben Avatar answered Sep 23 '22 01:09

Ben