Home » SQL & PL/SQL » SQL & PL/SQL » Return value between two characters (Oracle IDE 4.0.2.15.21)
Return value between two characters [message #629017] Tue, 02 December 2014 04:53 Go to next message
SteveShephard
Messages: 41
Registered: August 2012
Member
Hi all,

I have the following string

%^EMP_NO¬10^PA

Is it possible to write a select statement to return the value between %^EMP_NO¬ and ^PA (e.g. return the value 10)?.

Thanks

Steve
Re: Return value between two characters [message #629018 is a reply to message #629017] Tue, 02 December 2014 04:56 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Assuming that the strings always start and end with the characters above,
You could use instr and substr
Or two replaces
Or ltrim and rtrim
Or a regexp.

Re: Return value between two characters [message #629019 is a reply to message #629017] Tue, 02 December 2014 05:51 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Assuming that there are always digits between %^EMP_NO¬ and ^PA, then you could use DIGIT class :

SQL> WITH DATA AS(
  2  SELECT '%^EMP_NO¬10^PA' str FROM dual
  3  )
  4  SELECT regexp_substr(str, '[[:digit:]]+') str
  5  FROM DATA
  6  /

ST
--
10

SQL>

Else, you could use REPLACE as CM already suggested:

SQL> WITH DATA AS(
  2  SELECT '%^EMP_NO¬10^PA' str FROM dual
  3  )
  4  SELECT replace(replace(str, '%^EMP_NO¬', ''),'^PA','') str
  5  FROM DATA
  6  /

ST
--
10

SQL>

You could anyway use the old SUBSTR + INSTR approach. It might look lengthy, but it would be better than regular expressions in terms of performance as they are resource hungry and CPU intensive processes.

[Updated on: Tue, 02 December 2014 05:52]

Report message to a moderator

Previous Topic: Merge BLOB
Next Topic: Date function
Goto Forum:
  


Current Time: Thu Apr 25 20:12:41 CDT 2024