Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Reverse string in DB2 SQL

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

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Tue Aug 26, 2014 3:30 pm    Post subject: Reverse string in DB2 SQL
Reply with quote

Hi,

Do we have a reverse function in DB2 SQl to reverse the contents of the string variable .

Eg if var1 = 'PIUT'
expected output = 'TUIP'

i can't code a cobol program for the same as I am aware that COBOL has a reverse function. I ned to incorporate the logic in my SQL to achive this. If anyone aware of this please share your thought.

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

Rijit

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Wed Aug 27, 2014 6:36 pm    Post subject: Re: Reverse string in DB2 SQL
Reply with quote

Rijit wrote:
Hi,

Do we have a reverse function in DB2 SQl to reverse the contents of the string variable .

Eg if var1 = 'PIUT'
expected output = 'TUIP'

i can't code a cobol program for the same as I am aware that COBOL has a reverse function. I ned to incorporate the logic in my SQL to achive this. If anyone aware of this please share your thought.

Thanks


Hi any update techies?
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2422
Location: Netherlands, Amstelveen

PostPosted: Wed Aug 27, 2014 6:58 pm    Post subject:
Reply with quote

Not yet, but i'm thinking hard. That ok with you?
Back to top
View user's profile Send private message
Rijit

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Wed Aug 27, 2014 7:31 pm    Post subject:
Reply with quote

PeterHolland wrote:
Not yet, but i'm thinking hard. That ok with you?


Mee 2 thinking very hard. But not much footfall on my post so dissapointed..are all the techies sleeping icon_biggrin.gif icon_rolleyes.gif
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10202
Location: italy

PostPosted: Wed Aug 27, 2014 8:58 pm    Post subject: Reply to: Reverse string in DB2 SQL
Reply with quote

just curious what could be the business need to reverse something
Back to top
View user's profile Send private message
don.leahy

Active Member


Joined: 06 Jul 2010
Posts: 641
Location: Whitby, ON, Canada

PostPosted: Wed Aug 27, 2014 9:31 pm    Post subject:
Reply with quote

According to the version 10 SQL reference manual, you can define a UDF to do this:

Code:
Example 2: Define a scalar function that returns the text of an input string, in
reverse order:
CREATE FUNCTION REVERSE(INSTR VARCHAR(4000))
RETURNS VARCHAR(4000)
DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
BEGIN
DECLARE REVSTR, RESTSTR VARCHAR(4000) DEFAULT ’’;
DECLARE LEN INT;
IF INSTR IS NULL THEN
RETURN NULL;
END IF;
SET (RESTSTR, LEN) = (INSTR, LENGTH(INSTR));
WHILE LEN > 0 DO
SET (REVSTR, RESTSTR, LEN)
= (SUBSTR(RESTSTR, 1, 1) CONCAT REVSTR,
SUBSTR(RESTSTR, 2, LEN - 1),
LEN - 1);
END WHILE;
RETURN REVSTR;
END#
Back to top
View user's profile Send private message
Rijit

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Thu Aug 28, 2014 7:32 pm    Post subject:
Reply with quote

don.leahy wrote:
According to the version 10 SQL reference manual, you can define a UDF to do this:

Code:
Example 2: Define a scalar function that returns the text of an input string, in
reverse order:
CREATE FUNCTION REVERSE(INSTR VARCHAR(4000))
RETURNS VARCHAR(4000)
DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
BEGIN
DECLARE REVSTR, RESTSTR VARCHAR(4000) DEFAULT ’’;
DECLARE LEN INT;
IF INSTR IS NULL THEN
RETURN NULL;
END IF;
SET (RESTSTR, LEN) = (INSTR, LENGTH(INSTR));
WHILE LEN > 0 DO
SET (REVSTR, RESTSTR, LEN)
= (SUBSTR(RESTSTR, 1, 1) CONCAT REVSTR,
SUBSTR(RESTSTR, 2, LEN - 1),
LEN - 1);
END WHILE;
RETURN REVSTR;
END#


Hi this is not working and giving errors. icon_sad.gif
Back to top
View user's profile Send private message
Rijit

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Thu Aug 28, 2014 7:32 pm    Post subject:
Reply with quote

Can we use native DB2 function translate to do this string reverse?
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Thu Aug 28, 2014 8:10 pm    Post subject: Reply to: Reverse string in DB2 SQL
Reply with quote

Perhaps your field contains "srorre"?

What version of DB2 do you have? What exactly did you code and what exactly was the output?

You've ignored enrico's question.

DB2 function, no.
Back to top
View user's profile Send private message
don.leahy

Active Member


Joined: 06 Jul 2010
Posts: 641
Location: Whitby, ON, Canada

PostPosted: Thu Aug 28, 2014 8:11 pm    Post subject:
Reply with quote

Rijit wrote:
Can we use native DB2 function translate to do this string reverse?
Obviously not. Otherwise why would IBM have chosen to publish a REVERSE function as a sample UDF?
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Tue Sep 02, 2014 12:58 pm    Post subject:
Reply with quote

Hello,

If its a small string below should work,

Code:
SELECT                                               
STRIP(CAST( TRANSLATE('87654321','BOBBY', '12345678')
AS VARCHAR(8) ))                                   
FROM SYSIBM.SYSDUMMY1;                               
---------+---------+---------+---------+---------+---
--------------------                                 
---------+---------+---------+---------+---------+---
YBBOB                                               
          NUMBER OF ROWS SELECTED 1                 
---------+---------+---------+---------+---------+---
          SQL CODE IS   +100, SUCCESSFUL EXECUTION   


If its gonna be a bigger one, you should definitely use an UDF. Below links should help,
IDUG : REVERSE a string by using TRANSLATE (SQL)

DeveloperWorks : Full reverse: A REVERSE function that handles unicode

ExpertsExchange : Reverse String in DB2

Thanks,
Sushanth
Back to top
View user's profile Send private message
Rijit

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Tue Sep 02, 2014 2:30 pm    Post subject: Re: Reply to: Reverse string in DB2 SQL
Reply with quote

enrico-sorichetti wrote:
just curious what could be the business need to reverse something


Hi,

The business need is there because it is part of a business conversion we are doing in our project and we migrating data to a new database and we need to convert the data of a particular field(reverse) it .
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Tue Sep 02, 2014 2:39 pm    Post subject:
Reply with quote

icon_biggrin.gif Data Masking is it ?
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1219
Location: Israel

PostPosted: Tue Sep 02, 2014 4:49 pm    Post subject:
Reply with quote

enrico-sorichetti wrote:
just curious what could be the business need to reverse something

When using RTL alphabets, it is necessary to invert the column for the ORDER BY to work properly.
It may be a reason.
I asked a DBA and he said we use field procedures for this.
Back to top
View user's profile Send private message
Rijit

Active User


Joined: 15 Apr 2010
Posts: 168
Location: Pune

PostPosted: Tue Sep 02, 2014 7:18 pm    Post subject:
Reply with quote

sushanth bobby wrote:
Hello,

If its a small string below should work,

Code:
SELECT                                               
STRIP(CAST( TRANSLATE('87654321','BOBBY', '12345678')
AS VARCHAR(8) ))                                   
FROM SYSIBM.SYSDUMMY1;                               
---------+---------+---------+---------+---------+---
--------------------                                 
---------+---------+---------+---------+---------+---
YBBOB                                               
          NUMBER OF ROWS SELECTED 1                 
---------+---------+---------+---------+---------+---
          SQL CODE IS   +100, SUCCESSFUL EXECUTION   


If its gonna be a bigger one, you should definitely use an UDF. Below links should help,
IDUG : REVERSE a string by using TRANSLATE (SQL)

DeveloperWorks : Full reverse: A REVERSE function that handles unicode

ExpertsExchange : Reverse String in DB2

Thanks,
Sushanth


Hi this is working for me. Thanks a ton!
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1610
Location: NY,USA

PostPosted: Tue Sep 02, 2014 9:04 pm    Post subject:
Reply with quote

Also , I tried below and it works well too for larger field.

Code:
       SELECT                                               
STRIP(CAST( TRANSLATE('QPONMLKJIHGFEDCBA','ROHITUMARJIKAR', 'ABCDEFGHIJKLMNOPQ')
AS VARCHAR(20) ))                                   
FROM SYSIBM.SYSDUMMY1;

Output:
Code:
 1
 --------------
 RAKIJRAMUTIHOR
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
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am
No new posts Random Password (in string format) ge... ezhavendhan COBOL Programming 10 Mon Aug 29, 2016 3:18 pm
No new posts How to insert a lengthy string havin... vidyaa DB2 7 Thu Aug 25, 2016 5:20 pm
No new posts Which function does the Ampersand (&a... Andi1982 PL/I & Assembler 3 Wed Aug 24, 2016 2:05 pm
No new posts Need a help how to replace a string i... gurunath82 CLIST & REXX 12 Tue Jul 26, 2016 11:43 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us