MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shuffle the contents of a column

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
CJ
Beginner


Joined: 20 Jan 2003
Posts: 1
Topics: 1

PostPosted: Tue Jan 21, 2003 12:05 am    Post subject: Shuffle the contents of a column Reply with quote

Hi all,

I have a db2 column defined as character of 50 and it contains the lastname followed a comma and the firstname.Now my requirement is to shuffle the contents of the column to be firstname and a comma and lastname

Name

Code:


Speilberg,Steven
Cameroon,James
Carie,Jim


I want the the output to be

Code:

Steven,Speilberg
James,Cameroon
Jim,Carie


Can this be done in a single query?

Thanks

CJ
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12359
Topics: 75
Location: San Jose

PostPosted: Tue Jan 21, 2003 6:26 am    Post subject: Reply with quote

Cj,
The following sql will give you the results.I have used the scalar function POSSTR to find the position of the comma and then use the SUBSTR scalar function to move the contents. I also used the STRIP scalar function to remove any trailing spaces. You can remove it if don't require the strip of the blanks

Code:


SELECT  STRIP(SUBSTR(COLUMN_NAME,(POSSTR(COLUMN_NAME,',')+1),50))
        CONCAT ',' CONCAT                                         
        STRIP(SUBSTR(COLUMN_NAME,1,(POSSTR(COLUMN_NAME,',')-1))) 
        FROM TABLENAME                                           
        ;                                                         



check the following links for the scalar functions

POSSTR

SUBSTR

STRIP

Hope this helps...

cheers

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ajeykumar
Beginner


Joined: 29 Nov 2002
Posts: 8
Topics: 1
Location: Calcutta India

PostPosted: Tue Jan 21, 2003 7:27 am    Post subject: Reply with quote

Maybe you could also try it like this:

UPDATE Table_name
SET Column_name = SUBSTR(Col, LOCATE (',', Col), 50 ) || ',' ||
SUBSTR(Col, 1, LOCATE (',', Col))


Didn't get a chance to try it our myself, but it may work

Thanks
Ajey
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group