How to anticipate and escape single quote ' in oracle

前端 未结 2 953
轮回少年
轮回少年 2020-11-28 14:42

Suppose I have a column value as aaa\'gh it will throw error in oracle saying sql command not properly ended.

My question is if I don\'t know how many <

相关标签:
2条回答
  • 2020-11-28 15:27

    My simple approach with this problem, was always to just use

    replace(string, '''', '''''')
    

    but Lalit Kumar B solution, seems more sophisticated

    0 讨论(0)
  • 2020-11-28 15:34

    The best way is to use the quoting string literal technique. The syntax is q'[...]', where the "[" and "]" characters can be any of the following as long as they do not already appear in the string.

    • !
    • [ ]
    • { }
    • ( )
    • < >

    You don't have to worry about the single-quotation marks within the string.

    Suppose i have a column value as aaa'gh

    So you could simply write the SQL as,

    SELECT q'[aaa'gh]' FROM DUAL;
    

    It saves a lot of time for developers. Gone are those days when we(developers) used to verify the dynamic sql using dbms_output in development DB, just to make sure things are at place before moving into production.

    0 讨论(0)
提交回复
热议问题