If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Find a substr within a bracket

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-09-12, 01:37
ngaisteve1 ngaisteve1 is offline
Registered User
 
Join Date: Jan 2004
Posts: 5
Find a substr within a bracket

i have query which has the following result in the column.

Return Code (27)
Return Code (46)
Return Code (13)

I am trying to extra the value inside the bracket to become

27
46
13

I tried to use the substr but the most I have get it

27)
46)
13)

Can someone helps? Thanks.
Reply With Quote
  #2 (permalink)  
Old 01-09-12, 02:37
fengsun2 fengsun2 is offline
Registered User
 
Join Date: Nov 2011
Posts: 124
Try this:

values ( substr('return code(27)',posstr('return code(27)','(')+1,posstr('return code(27)',')')-posstr('return code(27)','(')-1 ) )
Reply With Quote
  #3 (permalink)  
Old 01-09-12, 03:03
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Removed repeated POSSTR functions.

Code:
------------------------------ Commands Entered ------------------------------
SELECT result_col
     , SUBSTR(result_col , lp_plus , POSSTR(result_col , ')') - lp_plus) AS extracted
 FROM  (VALUES 'Return Code (27)'
             , 'Return Code (46)'
             , 'Return Code (13)'
       ) result(result_col)
 CROSS JOIN LATERAL
       (VALUES POSSTR(result_col , '(') + 1 ) f(lp_plus)
;
------------------------------------------------------------------------------

RESULT_COL       EXTRACTED       
---------------- ----------------
Return Code (27) 27              
Return Code (46) 46              
Return Code (13) 13              

  3 record(s) selected.
Reply With Quote
  #4 (permalink)  
Old 01-09-12, 04:25
ngaisteve1 ngaisteve1 is offline
Registered User
 
Join Date: Jan 2004
Posts: 5
Hi thanks for all replies.

@tonkuma, I tried your query but I got an error: An unexpected token "CROSS" was found following ") result(result_col)". Expected tokens may include: "<space>"

@fengsun2, your query works without error, but I got:

27
27
27

but the result i need to get is:

27
46
13
Reply With Quote
  #5 (permalink)  
Old 01-09-12, 04:33
ngaisteve1 ngaisteve1 is offline
Registered User
 
Join Date: Jan 2004
Posts: 5
I managed to get it already using fengsun2 query. Thanks!
Reply With Quote
  #6 (permalink)  
Old 01-09-12, 05:59
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Originally Posted by ngaisteve1 View Post
...

@tonkuma, I tried your query but I got an error: An unexpected token "CROSS" was found following ") result(result_col)". Expected tokens may include: "<space>"

...
You must be using older DB2 version for LUW or DB2 for z/OS.

If so, replace "CROSS JOIN" with traditional join syntax(" , ").
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On