?
Solved

sql query

Posted on 2016-11-30
9
Medium Priority
?
69 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 32

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 32

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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 32

Accepted Solution

by:
Pawan Kumar earned 2000 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 38

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 38

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
A couple of weeks ago, my client requested me to implement a SSIS package that allows them to download their files from a FTP server and archives them. Microsoft SSIS is the powerful tool which allows us to proceed multiple files at same time even w…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to take different types of Oracle backups using RMAN.

649 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