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 > Replace characters

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-12, 04:42
Murthy_db2 Murthy_db2 is offline
Registered User
 
Join Date: Feb 2012
Posts: 3
Replace characters

Hi am getting the some special characters in my data

Sample data:- "3M™BathroomDisinfectantCleaner , i want to replace all specila characters with some default value, finally i have to get numerics and character values only.

Any one could you help me on this
Reply With Quote
  #2 (permalink)  
Old 02-06-12, 05:19
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Please define(or publish) "all specila characters" and "some default value".
Or define all characters "not specila characters".

"specila characters" may be different by Language, industry, Organization, so on.


Needless to say,
what DB2 version/release and platform OS are you using?
Reply With Quote
  #3 (permalink)  
Old 02-06-12, 05:47
Murthy_db2 Murthy_db2 is offline
Registered User
 
Join Date: Feb 2012
Posts: 3
Hi special characters may be @#$%^&™ and default value is "
Reply With Quote
  #4 (permalink)  
Old 02-06-12, 06:11
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
i want to replace all specila characters with some default value
Please use a TRANSLATE function.
TRANSLATE - IBM DB2 9.7 for Linux, UNIX, and Windows
Reply With Quote
  #5 (permalink)  
Old 02-06-12, 06:17
Murthy_db2 Murthy_db2 is offline
Registered User
 
Join Date: Feb 2012
Posts: 3
not able to handle the special character with translate function
Reply With Quote
  #6 (permalink)  
Old 02-06-12, 07:49
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
See the example.

Example 1: Tested on DB2 9.7.5 on Windows
Code:
------------------------------ Commands Entered ------------------------------
VALUES CAST( TRANSLATE( CAST('3M™BathroomDisinfectantCleaner' AS VARGRAPHIC(50))
                      , CAST('' AS GRAPHIC(1))
                      , CAST('@#$%^&™' AS GRAPHIC(7))
                      )
            AS VARCHAR(50)
           )
;
------------------------------------------------------------------------------

1                                                 
--------------------------------------------------
3M BathroomDisinfectantCleaner                    

  1 record(s) selected.
Reply With Quote
  #7 (permalink)  
Old 02-06-12, 08:00
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
OR

Example 2:
Code:
------------------------------ Commands Entered ------------------------------
VALUES CAST( TRANSLATE( '3M™BathroomDisinfectantCleaner'
                      , '      ' || CAST(' ' AS GRAPHIC(1))
                      , '@#$%^&' || CAST('™' AS GRAPHIC(1))
                      )
            AS VARCHAR(50)
           )
;
------------------------------------------------------------------------------

1                                                 
--------------------------------------------------
3M BathroomDisinfectantCleaner                    

  1 record(s) selected.
Reply With Quote
  #8 (permalink)  
Old 02-06-12, 08:25
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Replace all characters other than 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789' with a blank.

Example 3:
Code:
------------------------------ Commands Entered ------------------------------
VALUES CAST(
          TRANSLATE(
             '3M™BathroomDisinfectantCleaner@#$%^& Kanji(漢字) 000 ;:#$ ZZZ'
           , CAST('' AS GRAPHIC(1))
           , CAST(
                TRANSLATE(
                   '3M™BathroomDisinfectantCleaner@#$%^& Kanji(漢字) 000 ;:#$ ZZZ'
                 , ''
                 , 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
                 , ''
                ) /* TRANSLATE( */
              AS VARGRAPHIC(127)
             ) /* CAST( */
          ) /* TRANSLATE( */
        AS VARCHAR(100)
       ) /* CAST( */
;
------------------------------------------------------------------------------

1                                                                                                   
----------------------------------------------------------------------------------------------------
3M BathroomDisinfectantCleaner       Kanji     000      ZZZ                                         

  1 record(s) selected.

Last edited by tonkuma; 02-06-12 at 08:29. Reason: Add comments in sample code.
Reply With Quote
  #9 (permalink)  
Old 02-07-12, 01:02
Nithyasankari Nithyasankari is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks Tonkuma...I m also searching the same...It works fine..
Can u explain this translate function alone:

TRANSLATE(
'3M™BathroomDisinfectantCleaner@#$%^& Kanji(漢字) 000 ;:#$ ZZZ'
, ''
, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789'
, ''
)


Thanks in advance.
Reply With Quote
  #10 (permalink)  
Old 02-07-12, 01:20
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Please try to execute the function by yourself and see the result.
It will return all non alphanumeric characters(specified by 3rd argument) in the first argument.

Note: 2nd and 4th arguments are zero length string.
Then TRANSLATE function removed characters in 3rd argument from first argument.
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