Solved

sql query

Posted on 2016-11-30
9
57 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
Independent Software Vendors: 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

756 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