Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
How can we split a string by using SQL query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
narasridhar

New User


Joined: 12 Oct 2006
Posts: 32
Location: India

PostPosted: Tue Oct 24, 2006 2:15 pm    Post subject: How can we split a string by using SQL query
Reply with quote

How can we split a string by using SQL query? Please give a sample query with the example
Back to top
View user's profile Send private message

ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Oct 24, 2006 5:22 pm    Post subject:
Reply with quote

use SUBSTR function .....

Select SUBSTR(Name,1,5) from table; here it will split the Col Name value strting from position 1 and 5 chars ......
Back to top
View user's profile Send private message
narasridhar

New User


Joined: 12 Oct 2006
Posts: 32
Location: India

PostPosted: Tue Oct 24, 2006 5:38 pm    Post subject:
Reply with quote

ashimer wrote:
use SUBSTR function .....

Select SUBSTR(Name,1,5) from table; here it will split the Col Name value strting from position 1 and 5 chars ......


if we dint know the number of charecters to split ... the split will be based on the space middle of the name that case what we need to do?
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Oct 24, 2006 5:47 pm    Post subject: Re: Regarding strings query
Reply with quote

can u b more specific ....
for using substr u need to knw the strting position n length ....
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6970
Location: porcelain throne

PostPosted: Tue Oct 24, 2006 6:58 pm    Post subject:
Reply with quote

suggest you retrieve the complete column from DB2 and use a COBOL UNSTRING command. DB2 does not have a 'parse' command, and that is what you are looking for.

if you have a constant business need for spliting names, suggest you redefine your database.
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Tue Oct 24, 2006 10:40 pm    Post subject: Re: Regarding strings query
Reply with quote

NARA,

You mean something like this?

Code:

DECLARE GLOBAL TEMPORARY TABLE TEMP_TBL                     
  (                                                         
       NAME                    CHAR(20)                     
   )                                                         
;                                                           
INSERT INTO SESSION.TEMP_TBL (NAME) VALUES ('GEORGE BUSH')   
;                                                           
INSERT INTO SESSION.TEMP_TBL (NAME) VALUES ('NARA SRIDHAR') 
;
                                                           
SELECT SUBSTR(NAME,1,LOCATE(' ',NAME) - 1) AS FIRST_NAME,   
       SUBSTR(NAME,LOCATE(' ',NAME) + 1)   AS LAST_NAME     
  FROM SESSION.TEMP_TBL                                     
;


Results:

Code:

   +---------------------------------------------+
   |      FIRST_NAME      |      LAST_NAME       |
   +---------------------------------------------+
 1_| GEORGE               | BUSH                 |
 2_| NARA                 | SRIDHAR              |
   +---------------------------------------------+
 


Dave
Back to top
View user's profile Send private message
narasridhar

New User


Joined: 12 Oct 2006
Posts: 32
Location: India

PostPosted: Wed Oct 25, 2006 1:10 pm    Post subject:
Reply with quote

Thanks DavidatK
Back to top
View user's profile Send private message
MCEVOY

New User


Joined: 21 Nov 2005
Posts: 18

PostPosted: Wed Oct 25, 2006 4:18 pm    Post subject: Re: How can we split a string by using SQL query
Reply with quote

Try

SELECT SUBSTR(column,1,POSSTR(column,' ')-1),
SUBSTR(column,POSSTR(column,' ')+1)

The POSSTR function finds the position of one string within another.
Back to top
View user's profile Send private message
saraswathis

New User


Joined: 06 Sep 2006
Posts: 8

PostPosted: Wed May 02, 2007 5:49 pm    Post subject: POSSTR is not working fully
Reply with quote

hi
Me too have the problem in splitting a string with spaces. But while doing in SPUFI (MF) in SUBSTR function as

SUBSTR(P.P_PAYEE_NAME,1,POSSTR(P.P_PAYEE_NAME,' ')-1)
or
SUBSTR(P.P_PAYEE_NAME,1,(POSSTR(P.P_PAYEE_NAME,' ')-1))

i m getting -138 error - THE SECOND OR THIRD ARGUMENT OF THE SUBSTR FUNCTION IS OUT OF RANGE.

pl. help me to solve this.

Thnks in adv.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Wed May 02, 2007 8:19 pm    Post subject:
Reply with quote

Hello,

Your code does not match David's solution. . .

Try changing this
Quote:
SUBSTR(P.P_PAYEE_NAME,1,POSSTR(P.P_PAYEE_NAME,' ')-1)
or
SUBSTR(P.P_PAYEE_NAME,1,(POSSTR(P.P_PAYEE_NAME,' ')-1))
to follow David's example
Code:
SELECT SUBSTR(NAME,1,LOCATE(' ',NAME) - 1) AS FIRST_NAME,   
       SUBSTR(NAME,LOCATE(' ',NAME) + 1)   AS LAST_NAME     
  FROM SESSION.TEMP_TBL               


Notice that the example works but does not use POSSTR.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts query to fetch record which has only ... maxsubrat DB2 12 Mon Dec 11, 2017 5:03 pm
No new posts How to query MAXGENS of an existing P... Chuchulo Hatyzak JCL & VSAM 4 Wed Nov 22, 2017 10:23 pm
No new posts Native SQL Query kishpra DB2 1 Wed Nov 22, 2017 8:38 pm
No new posts Split files upto certain limit based ... bubbu75 DFSORT/ICETOOL 5 Tue Nov 14, 2017 11:11 pm
No new posts Query on IEFBR14 with GDG Ashishpanpaliya JCL & VSAM 4 Tue Nov 07, 2017 8:34 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us