- 3,714,819 Users
- 2,242,634 Discussions
- 7,845,078 Comments
Forum Stats
Discussions
Categories
- 12 Data
- 362.2K Big Data Appliance
- 6 Data Science
- 1.5K Databases
- 447 General Database Discussions
- 3.7K Java and JavaScript in the Database
- 22 Multilingual Engine
- 487 MySQL Community Space
- 3 NoSQL Database
- 7.6K Oracle Database Express Edition (XE)
- 2.7K ORDS, SODA & JSON in the Database
- 415 SQLcl
- 42 SQL Developer Data Modeler
- 184.8K SQL & PL/SQL
- 21K SQL Developer
- 1.8K Development
- 3 Developer Projects
- 32 Programming Languages
- 135K Development Tools
- 7 DevOps
- 3K QA/Testing
- 236 Java
- 4 Java Learning Subscription
- 10 Database Connectivity
- 65 Java Community Process
- Java 25
- 8 Java APIs
- 141.1K Java Development Tools
- 5 Java EE (Java Enterprise Edition)
- 153K Java Essentials
- 134 Java 8 Questions
- 86.2K Java Programming
- 270 Java Lambda MOOC
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 10 Java SE
- 13.8K Java Security
- 3 Java User Groups
- 22 JavaScript - Nashorn
- 18 Programs
- 118 LiveLabs
- 28 Workshops
- 9 Software
- 3 Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 3 Deutsche Oracle Community
- 10 Español
- 1.9K Japanese
- 2 Portuguese
how to get specific values from a string

Hi All,
I have a requirement where i will be having a particular string like as mentioned below:
205_CL_GRP_DTL
now i have to extract the value by ignoring number from the front and the first _
I have used below sql statement:
select regexp_replace('205_CL_GRP_DTL', '[0-9]', '') from dual;
but it is giving me values like _CL_GRP_DTL
but my requirement is to get string value as CL_GRP_DTL
Could someone please suggest on the same.
Answers
-
with inp as
(select '205_CL_GRP_DTL' pstring from dual)
select substr(inp.pstring,instr(inp.pstring,'_')+1)
from inp
i.e take everyting in the string after the first '_'
-
Hi,
now i have to extract the value by ignoring number from the front and the first _
I have used below sql statement:
select regexp_replace('205_CL_GRP_DTL', '[0-9]', '') from dual;
That doesn't just remove digits from the front (that is, the beginning) of the string; it will remove all digits, no matter where they occur. If you want to remove digits only from the beginning of the string, then anchor the expression to the beginning or the string, using ^, like this:
REGEXP_REPLACE (str, '^[0-9]+_?')
The pattern this looks for is
- At the beginning of the string ('^')
- any digits ('[0-9]): one or more of them ('+')
- an underscore character ('_'), if that's the next character ('?')
What would you want if the string started with digits but no underscore, e.g. '2FOO_34_BAR'?
Instead of '[0-9]' you could also use '\d', like this
REGEXP_REPLACE (str, '^\d+_')
-
If you want to use regex, you'd probably be better off with regexp_substr :
select regexp_substr('205_CL_GRP_DTL', '^(\d*_?)(.*)$', 1, 1, 'i', 2) from dual
Note, I've made some assumptions here though (including that you aren't on anything older than 11g), that your string MIGHT start with numbers and an underscore.
Regex explained :
^(\d*_?)(.*)$
Assert position at the beginning of the string «^»
Match the regex below and capture its match into backreference number 1 «(\d*_?)»
Match a single character that is a “digit” (any decimal number in any Unicode script) «\d*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “_” literally «_?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the regex below and capture its match into backreference number 2 «(.*)»
Match any single character that is NOT a line break character (line feed) «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert position at the end of the string, or before the line break at the end of the string, if any (line feed) «$»
-
There are really (at least) two questions you need to answer, to clarify your question. For both, you may answer "this cannot happen in my data". But if it can, you must explain the desired handling.
First, as Mr. Kulash explained, what if you have digits at the beginning, but not followed immediately by an underscore?
Second, what if you do have underscores, but the "first token" is not all digits? Like ABC_BLAH_BLAH_BLAH - do you still want the first "token" removed, or should this string be returned as is?
(OR are ALL your strings in the format {one or more digits}_{something else}?)