If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Identify and replace \n (hard return) in record column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-05-06, 05:24
ggnanaraj ggnanaraj is offline
Registered User
 
Join Date: Aug 2002
Location: Chennai, India
Posts: 171
Identify and replace \n (hard return) in record column

I need to identify & replace all occurrences of hard return characters in a VARCHAR defined attribute/column.

How do I do this?

For example, the content of the record is as follows: (multiple lines)
No provider for smtp
javax.mail.NoSuchProviderException
No provider for smtp ( Cause - No provider

The above I need to change to ...(1 line)
No provider for smtp javax.mail.NoSuchProviderException No provider for smtp ( Cause - No provider

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 10-05-06, 08:08
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You are going to need to write a UDF (User Defined Function).

Andy
Reply With Quote
  #3 (permalink)  
Old 10-05-06, 08:12
ggnanaraj ggnanaraj is offline
Registered User
 
Join Date: Aug 2002
Location: Chennai, India
Posts: 171
Quote:
Originally Posted by ARWinner
You are going to need to write a UDF (User Defined Function).

Andy
Thanks for the input.

Can you please give additional information.

TIA.
Reply With Quote
  #4 (permalink)  
Old 10-05-06, 08:26
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You need to create something like this:

CREATE function andy.UDF_STRIP_CR( strIn VARCHAR(8000) )
RETURNS VARCHAR(8000)
LANGUAGE SQL DETERMINISTIC
NO EXTERNAL ACTION CALLED ON NULL INPUT
------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
F1: BEGIN ATOMIC
DECLARE strOut VARCHAR(8000) ;
DECLARE nLen, nCnt INT ;

SET strOut = '' ;
SET nLen = LENGTH(strIn) ;
SET nCnt = 1 ;

IF ( strIn IS NULL ) THEN
RETURN NULL ;
END IF ;

WHILE ( nCnt <= nLen ) DO
IF (( SUBSTR(strIn,nCnt,1) <> CHR(13)) and ( SUBSTR(strIn,nCnt,1) <> CHR(10)))
THEN SET strOut = strOut || SUBSTR(strIn,nCnt,1);
END IF ;
SET nCnt = nCnt + 1 ;
END WHILE ;

RETURN strOut ;
END


Then you use it to translate the data in a query.

select andy.udf_strip_cr(text) from mytable.


Andy
Reply With Quote
  #5 (permalink)  
Old 10-05-06, 10:25
ggnanaraj ggnanaraj is offline
Registered User
 
Join Date: Aug 2002
Location: Chennai, India
Posts: 171
Thanks a million, Andy!

That solved the issue.

However, can we also use the REPLACE function?

Thanks again for the timely help. God bless.
Reply With Quote
  #6 (permalink)  
Old 10-05-06, 10:34
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes, you can use the REPLACE function.

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On