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

Reverse string in DB2 SQL


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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
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: 2481
Location: Netherlands, Amstelveen

PostPosted: Wed Aug 27, 2014 6:58 pm
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Wed Aug 27, 2014 8:58 pm
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: 765
Location: Whitby, ON, Canada

PostPosted: Wed Aug 27, 2014 9:31 pm
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
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
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

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Aug 28, 2014 8:10 pm
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: 765
Location: Whitby, ON, Canada

PostPosted: Thu Aug 28, 2014 8:11 pm
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: 1020
Location: India

PostPosted: Tue Sep 02, 2014 12:58 pm
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
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: 1020
Location: India

PostPosted: Tue Sep 02, 2014 2:39 pm
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: 1353
Location: Israel

PostPosted: Tue Sep 02, 2014 4:49 pm
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
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

Global Moderator


Joined: 21 Sep 2010
Posts: 3053
Location: NYC,USA

PostPosted: Tue Sep 02, 2014 9:04 pm
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
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 file manager is doing string conversion IBM Tools 3
No new posts Search string in job at regular Spool... CLIST & REXX 0
Search our Forums:

Back to Top