Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PL/SQL and the pesky single quote

Tips and Tricks

PL/SQL and the pesky single quote

by  taupirho  Posted    (Edited  )
You know when you're constructing strings in PL/SQL and you try and concatenate say a varchar variable with a string, but the variable needs to be enclosed in single quotes. You want something like,

SELECT x FROM table1 WHERE z = '123'

where 123 is held as a variable, so you have to do something like this :-


sql_select := 'SELECT x FROM table1 WHERE z = ' || '''' || :some_variable || '''';

How many single quotes do you need is it two, three - four?

Its messy so why not get the DECIMAL representation of a single quote and use that with the CHR function instead. Get the DECIMAL value of the single quote (or any other ASCII character) using:-

SQL> SELECT ASCII('''') FROM DUAL; -> Answer 39

Yup, 4 quotes required - see what I mean !


Now all you need is something like.

DECLARE
qte varchar2(1) := CHR(39);
BEGIN
sql_select := 'SELECT x FROM table1 WHERE z = ' || qte ||:some_variable || qte;
END;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top