oracle小技巧:字符串原样输出
在sql查询中,我们经常需要原样输出字符串,如果字符串中含有大量的单引号、双引号或者特殊字符,那么需要用单引号转义拼接字符串,这样会非常的麻烦。
oracle提供了一个Q-quote的表达式来原样输出字符串。
SELECT Q'[I'm a boy,my name is 'david']' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'(I'm a boy,my name is 'david')' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'{I'm a boy,my name is 'david'}' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'|I'm a boy,my name is 'david'|' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'aI'm a boy,my name is 'david'a' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'2I'm a boy,my name is 'david'2' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'@I'm a boy,my name is 'david'@' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'*I'm a boy,my name is 'david'*' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'#I'm a boy,my name is 'david'#' FROM DUAL; --结果:I'm a boy,my name is 'david' SELECT Q'[[@#$%^&*~?/" ']]' FROM DUAL; --结果:[@#$%^&*~?/" ']
小结: (1)Q'后跟起始分隔符,起始分隔符后的字符串原样输出,起始分隔符必须有配对的结束分隔符。
(2)分隔符可以为数字、字母、特殊字符。但'&'不能作为分隔符,因为'&'意思是传入参数。
(3)'['、'('、'{'作为分隔符,必须以']'、')'、'}'结束。