IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How can we split a string by using SQL query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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
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
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
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: 6966
Location: porcelain throne

PostPosted: Tue Oct 24, 2006 6:58 pm
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
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
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
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
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

Moderator Emeritus


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

PostPosted: Wed May 02, 2007 8:19 pm
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts RC query -Time column CA Products 3
Search our Forums:

Back to Top