Solved

sql query

Posted on 2016-11-30
9
60 Views
Last Modified: 2016-11-30
In the attached table the column remark has the data as shown. I need to extract the part of the string after the alphabet "k" where "k" is included and in the third row it should return null as "k" is not there. Please help me with the query to fetch the data from this column. the table name is Table1.Table1
0
Comment
Question by:sam shah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41906962
Try..

SELECT * FROM
(
  select (CASE WHEN instr(remark,'k') > 0 THEN substr(remark,instr(remark,'k',-1)) ELSE NULL END) Extensions from YourTable
)
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41906966
Testing..

WITH CTE AS
(
 
   SELECT 'ab12 k1234567' remark FROM DUAL UNION ALL
   SELECT 'cd13 k1234'  FROM DUAL UNION ALL
   SELECT 'ef14 1234'  FROM DUAL UNION ALL
   SELECT ''  FROM DUAL
)    
SELECT * FROM 
(
  select (CASE WHEN instr(remark,'k') > 0 THEN substr(remark,instr(remark,'k',-1)) ELSE NULL END) Extensions from CTE
)

Open in new window


Output



 	EXTENSIONS
1	k1234567
2	k1234
3	NULL
4	NULL

Open in new window


Hope it helps !!
0
 

Author Comment

by:sam shah
ID: 41906972
where is the table name in the above code and what is "WITH CTE AS" for? can you plz explain a bit.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41906973
That I just use for the  testing purpose. Basically creating a table at runtime..

You can use below- <<Replace CTE with your tablename>>

SELECT * FROM 
(
  select (CASE WHEN instr(remark,'k') > 0 THEN substr(remark,instr(remark,'k',-1)) ELSE NULL END) Extensions from CTE
)

Open in new window

0
 

Author Closing Comment

by:sam shah
ID: 41906978
Thanks a lot.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41907017
regexp is also a good choice ...

if you only want the numbers
with sample as (
  select 'ab12 k123567' x from dual
  union all select 'cd13 k1234' from dual
  union all select 'ef14 1234' from dual)
select regexp_substr(x, 'k(\d*)', 1, 1, 'i', 1) from sample

Open in new window


if you want k and the numbers
with sample as (
  select 'ab12 k123567' x from dual
  union all select 'cd13 k1234' from dual
  union all select 'ef14 1234' from dual)
select regexp_substr(x, 'k(\d*)') from sample

Open in new window


leaving a question open some time allows time for alternatives :)
0
 

Author Comment

by:sam shah
ID: 41907112
select remarks, regexp_substr(remarks,'\d+$') from table1;  

i used this but it is not returning the value along with "k". it is just returning the digits.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41907277
Is it possible to have more than one 'k'?
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41907323
\d > indicates a digit
+ indicates 1 or more 
$ indicates at the end of string

Open in new window


http://www.regular-expressions.info/refquick.html

so you want the last digits ?
your regex doesn't include a k

the k with digits if in the end of the line :  

with sample as (
  select 'ab12 k123567' x from dual
  union all select 'cd13 k5678 k1234' from dual
  union all select 'ef14 1234' from dual)
select regexp_substr(x, 'k\d+$') from sample

Open in new window


if the k and digits are not at the end of the line, then the row is not found
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
StoredProcedure to JSON query faulty syntax 2 45
error starting form builder in 11g 2 47
how to solve "recursive" database tables 2 52
comparing two rows 10 39
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question