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 > how to replace quotes in db2 sql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-22-09, 20:55
t9119 t9119 is offline
Registered User
 
Join Date: Nov 2009
Posts: 3
how to replace quotes in db2 sql

the replace function cannot be used to replace single or double quotes. cna you let me know if there is any way to replace quotes with other character. thanks in advance
Reply With Quote
  #2 (permalink)  
Old 11-22-09, 21:09
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Find out the ascii hex value, and then replace it with another hex value or space. For example X'22' is a double-quote.
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 11-22-09, 23:28
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
you have to use double quotes not a single...

Kara
Reply With Quote
  #4 (permalink)  
Old 11-23-09, 13:31
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can replace single or double quotes just fine. The only thing to remember is to use proper escaping. Escaping in SQL is to double the escaped character, i.e. a single quote inside a string is ''.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 11-23-09, 17:34
t9119 t9119 is offline
Registered User
 
Join Date: Nov 2009
Posts: 3
can i use the replace function in db2 to do that
Reply With Quote
  #6 (permalink)  
Old 11-23-09, 18:20
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If this was a question, the answer is: yes, you can do that.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 11-24-09, 17:41
t9119 t9119 is offline
Registered User
 
Join Date: Nov 2009
Posts: 3
what about using regular expression in replace or translate function like can i use something like ^[A-Za-z0-9] if i want to replace anything other than alpha numeric.

Thanks
Reply With Quote
  #8 (permalink)  
Old 11-25-09, 09:03
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
TRANSLATE and REPLACE don't work with regular expressions. But you can roll your own function if you need to: Bringing the Power of Regular Expression Matching to SQL
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 02-15-12, 08:49
dr_te_z dr_te_z is offline
Registered User
 
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
Google found this old thread but the answer is not here. So here you go:
Code:
db2 "values replace(varchar('hoi''s'),x'27',space(1))"
The single quote will be replaced by a space
"hoi's" := "hoi s"
Reply With Quote
  #10 (permalink)  
Old 02-15-12, 08:55
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by dr_te_z View Post
Google found this old thread but the answer is not here. So here you go:
Code:
db2 "values replace(varchar('hoi''s'),x'27',space(1))"
The single quote will be replaced by a space
"hoi's" := "hoi s"
How about this as I suggested over two years ago:
Code:
db2 "values replace(varchar('hoi''s'),x'27',x'20')"

1
-----
hoi s

  1 record(s) selected.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #11 (permalink)  
Old 02-15-12, 14:36
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Or if someone doesn't like to use ASCII code points:
Code:
replace(varchar('hoi''s'), '''', ' ')
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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