By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,333 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,333 IT Pros & Developers. It's quick & easy.

What's the escape char in DB2 SQL

P: n/a
hi all,

I want to output something this way:
Select 'This is 'A' book' from ...
how to do this?
Mar 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tiger Champ wrote:
hi all,

I want to output something this way:
Select 'This is 'A' book' from ...
how to do this?
Double the single-quote:

SELECT 'This is ''A'' book' FROM ...

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Mar 29 '07 #2

P: n/a
Thanks Knut, but I hope I can print right same as I want:
This is 'a' book
Can I do this? Actually I want to use this Select to generate another query,
and there's some constant string in it, so I need to do this.

"Knut Stolze" <st****@de.ibm.com>
??????:57*************@mid.individual.net...
Tiger Champ wrote:
>hi all,

I want to output something this way:
Select 'This is 'A' book' from ...
how to do this?

Double the single-quote:

SELECT 'This is ''A'' book' FROM ...

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

Mar 29 '07 #3

P: n/a
Tiger Champ wrote:
Thanks Knut, but I hope I can print right same as I want:
This is 'a' book
What does "print" mean in your context?
Can I do this? Actually I want to use this Select to generate another
query, and there's some constant string in it, so I need to do this.
The escaping is only necessary when the string is interpreted somehow:
either when you construct the string from literals, or when you give it to
DB2 to prepare the statement. Once interpreted, DB2 has internally a
single-quote character and there are no string delimiters any longer
relevant.

Here is an example:

$ db2 "VALUES 'This is a ''string'''"

1
------------------
this is a 'string'

1 record(s) selected.

When DB2 parses this SQL statement, it finds the first single-quote and
knows that you provide a string literal. Then it searches for the next
single-quote. Following that is another single-quote, indicating that only
a _single_ single-quote should remain and that the string literal is not
yet finished. So you have a nested single-quote in the string. The same
happens with the 2nd escaped single-quote. Finally, DB2 encounters the
last single-quote. That does not escape another single-quote so that it
marks the end of the string literal.

That's just the parsing/interpretation process. Internally, you just have a
string as the one shown in the result for my query above.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Mar 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.