Home » SQL & PL/SQL » SQL & PL/SQL » SQL Query (10g)
SQL Query [message #587204] Thu, 13 June 2013 04:02 Go to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
Hi All,

Please can any one help me to frame a query for the below values . Thanks in Advance.
I have few records in a below format.

1) 'CODE#ATC#YEAR#1980#NUM#4229#VOL#80##'
2) 'CODE#NSWLR#YEAR#1985#PAGE#158#VOL#4##'
3) 'CODE#WASC#YEAR#1981#MONTH#07#DAY#24#'
4) 'CODE#RPC#YEAR#1919#PAGE#296#VOL#36#'

I need only the Page from the above records.
For example if i take this record

'CODE#NSWLR#YEAR#1985#PAGE#158#VOL#4##' -- I need to extract only the Page from this like 158.

If the record doesn't contain Page then i need to update as null.
Re: SQL Query [message #587205 is a reply to message #587204] Thu, 13 June 2013 04:03 Go to previous messageGo to next message
cookiemonster
Messages: 13968
Registered: September 2008
Location: Rainy Manchester
Senior Member
use instr and substr
Re: SQL Query [message #587208 is a reply to message #587205] Thu, 13 June 2013 04:07 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
But i m not getting a exact result set Sad
Re: SQL Query [message #587213 is a reply to message #587208] Thu, 13 June 2013 04:19 Go to previous messageGo to next message
cookiemonster
Messages: 13968
Registered: September 2008
Location: Rainy Manchester
Senior Member
What exact result do you want and what code are you using?
Re: SQL Query [message #587214 is a reply to message #587213] Thu, 13 June 2013 04:23 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
Hi, I m using this query
Select Substr('CODE#NSWLR#YEAR#1985#PAGE#158#VOL#4##', 1,
INSTR('CODE#NSWLR#YEAR#1985#PAGE#158#VOL#4##','#',-2)) as page
From Dual;
From this i need to extract only 158.
Re: SQL Query [message #587216 is a reply to message #587214] Thu, 13 June 2013 04:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You do not search for #, you search for PAGE#; then from this point you search for next #, the page number is between.

Regards
Michel
Re: SQL Query [message #587217 is a reply to message #587214] Thu, 13 June 2013 04:32 Go to previous messageGo to next message
cookiemonster
Messages: 13968
Registered: September 2008
Location: Rainy Manchester
Senior Member
So why have you put the 2nd parameter to substr as 1? You don't want the first several characters in the string do you?
And I'd be telling instr to look for 'PAGE' not '#'
Re: SQL Query [message #587219 is a reply to message #587216] Thu, 13 June 2013 04:38 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
Please help me with Query..
Re: SQL Query [message #587220 is a reply to message #587219] Thu, 13 June 2013 04:42 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
That's right guys, do his work for him. don't bother actually helping him to understand how to solve it himself, what's the point? Wink
Re: SQL Query [message #587227 is a reply to message #587220] Thu, 13 June 2013 05:23 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Apart from INSTR + SUBSTR option, here's one that uses regular expressions. According to provided examples,
regexp_substr(column_name, '\d+', 1, 2)
does the job for records that satisfy
where instr(column_name, '#PAGE#') > 0 
condition
Re: SQL Query [message #587229 is a reply to message #587227] Thu, 13 June 2013 05:53 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
Thanks a lot Littlefoot ..But This is not working in this scenario if my record looks like this 'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#340#'
Re: SQL Query [message #587231 is a reply to message #587229] Thu, 13 June 2013 06:03 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
As I said, me

According to provided examples ...

You have data, not us. We only know what you tell. So, you're back on INSTR + SUBSTR track or combination
replace(regexp_substr(column_name, 'PAGE#\d+'), 'PAGE#')
or something else.
Re: SQL Query [message #587259 is a reply to message #587231] Thu, 13 June 2013 09:13 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
Thanks a lot Smile I got a result set ..Checking for Various scenarios if my page comes like 'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#34-0#' ,'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#340a#' etc . Hope it works fine .
Re: SQL Query [message #587262 is a reply to message #587231] Thu, 13 June 2013 09:36 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Littlefoot wrote on Thu, 13 June 2013 07:03
or something else.


On 11g REGEXP_SUBSTR supports sub-expressions, so replace is not needed:

with t as (
           select  'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#34-0#' ,'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#340a#' column_name
             from  dual
          )
select  regexp_substr(column_name, 'PAGE#(\d+)',1,1,null,1)
  from  t
/

REG
---
340

SQL> 


SY.
Re: SQL Query [message #587263 is a reply to message #587262] Thu, 13 June 2013 09:49 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
I m getting error like Sad
ORA-00939: too many arguments for function
00939. 00000 - "too many arguments for function"
*Cause:
*Action:
Error at Line: 21 Column: 12
Re: SQL Query [message #587264 is a reply to message #587263] Thu, 13 June 2013 09:50 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Must be because you're on 10g, while Solomon showed a 11g solution.
Re: SQL Query [message #587265 is a reply to message #587263] Thu, 13 June 2013 09:51 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
we can't debug code we can not see.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: SQL Query [message #587266 is a reply to message #587259] Thu, 13 June 2013 09:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
vnithya wrote on Thu, 13 June 2013 16:13
Thanks a lot Smile I got a result set ..Checking for Various scenarios if my page comes like 'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#34-0#' ,'CODE#MacG Cop Cas#YEAR#1923-28#PAGE#340a#' etc . Hope it works fine .


So post it.

Regards
Michel

Re: SQL Query [message #587268 is a reply to message #587266] Thu, 13 June 2013 09:59 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
I am sorry ..I am not getting the result using this query
SELECT TRUNC(REPLACE(REGEXP_SUBSTR('CODE#MacG Cop Cas#YEAR#1923-28#PAGE#34-0#', 'PAGE#\d+'), 'PAGE#'))
FROM dual;
Re: SQL Query [message #587270 is a reply to message #587268] Thu, 13 June 2013 10:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
1/ What do you get
2/ What do you want to get
3/ In the end, go to INSTR and REPLACE.

Regards
Michel
Re: SQL Query [message #587271 is a reply to message #587270] Thu, 13 June 2013 10:04 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
1) I get as a result 34
2) But i need 34-0
Re: SQL Query [message #587273 is a reply to message #587271] Thu, 13 June 2013 10:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You make NO effort, just copy what we give you without even trying to understand the query (and even not able to post the execution of it).
SQL> SELECT REPLACE(REGEXP_SUBSTR('CODE#MacG Cop Cas#YEAR#1923-28#PAGE#34-0#', 'PAGE#[^#]+'), 'PAGE#')
  2  FROM dual;
REPL
----
34-0

Regards
Michel
Re: SQL Query [message #587276 is a reply to message #587273] Thu, 13 June 2013 10:23 Go to previous messageGo to next message
userora
Messages: 63
Registered: December 2012
Location: Chennai
Member
Smile Working fine Michel Thanks a lot for your help and even for others.
Re: SQL Query [message #587277 is a reply to message #587276] Thu, 13 June 2013 11:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Do you understand why it is working fine?

Regards
Michel
Re: SQL Query [message #587278 is a reply to message #587277] Thu, 13 June 2013 11:40 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Michel Cadot wrote on Thu, 13 June 2013 09:21
Do you understand why it is working fine?

Regards
Michel


It is working fine because he got the desired results & did not have to write the SQL himself.
Re: SQL Query [message #587279 is a reply to message #587278] Thu, 13 June 2013 12:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Laughing
Re: SQL Query [message #587434 is a reply to message #587278] Fri, 14 June 2013 14:55 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
6 guys trying to help the poster, 3 are posting hints with examples, other 3 are asking to follow forum guidelines, lest the poster shouldn't be helped. Who is confusing whom? Why not be clear at the 1st step.
Re: SQL Query [message #587436 is a reply to message #587434] Fri, 14 June 2013 15:14 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Lalit Kumar B wrote on Fri, 14 June 2013 20:55
6 guys trying to help the poster, 3 are posting hints with examples, other 3 are asking to follow forum guidelines,
I disagree.
Quote:
lest the poster shouldn't be helped. Who is confusing whom?
Exactly what point are you attempting to make here?
Quote:
Why not be clear at the 1st step.

What part of cookie's first post do you find unclear?

[Updated on: Fri, 14 June 2013 15:14]

Report message to a moderator

Re: SQL Query [message #587438 is a reply to message #587278] Fri, 14 June 2013 15:18 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Laughing

Dumb she.
Re: SQL Query [message #587440 is a reply to message #587436] Fri, 14 June 2013 15:29 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
BlackSwan posted forum guidelines almost after a lot of replies, however, the poster repeatedly did not follow. Had it been very clear in the beginning itself, then no point of so much of effort. I appreciate the effort that was made to help the poster.

But, look in the end, the poster simply left the thread without even caring to tell what she learnt or how she fixed the issue that she faced earlier.It must be a win-win situation, where the helper should be satisfied in the end that the poster completely understood what was being explained.
Re: SQL Query [message #587443 is a reply to message #587440] Fri, 14 June 2013 15:38 Go to previous message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Lalit Kumar B wrote on Fri, 14 June 2013 21:29
BlackSwan posted forum guidelines almost after a lot of replies,
OK, that's one person. You said three posted guidelines. I disagree.
Quote:
however, the poster repeatedly did not follow.
Correct, very rude.
Quote:
Had it been very clear in the beginning itself, then no point of so much of effort.
Had what been clear? The question? The first answer? The request for the OP to follow some very simple guidelines?
Quote:
I appreciate the effort that was made to help the poster.
You have no idea how happy that makes me, my heart is filled with joy and my life full of light with that knowledge.

Quote:
But, look in the end, the poster simply left the thread without even caring to tell what she learnt
Yes, very, very rude of her.
Quote:
or how she fixed the issue that she faced earlier.
If indeed, she actually did fix it.
Quote:
It must be a win-win situation, where the helper should be satisfied in the end that the poster completely understood what was being explained.
Then the first step would be for people to follow the rules don't you think?

[Updated on: Fri, 14 June 2013 15:39]

Report message to a moderator

Previous Topic: Issue with Rownum
Next Topic: Getting the next data with loop
Goto Forum:
  


Current Time: Sat Aug 30 21:11:28 CDT 2025