Solved

sql query

Posted on 2016-11-30
9
22 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Expert Comment

by:Pawan Kumar Khowal
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
 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 31

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now