How to enter a single quotation mark in Oracle?

本文介绍了在Oracle数据库中如何正确地显示单引号。提供了两种实用的方法:一种是使用额外的单引号字符作为转义字符;另一种是通过变量或CHR(39)函数来插入单引号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Answer: Although this may be a undervalued question, I got many a search for my blog with this question. This is where I wanted to address this question elaborately or rather in multiple ways.

 

Method 1


The most simple and most used way is to use a single quotation mark with two single quotation marks in both sides.

 

SELECT 'test single quote''' from dual;

The output of the above statement would be:
test single quote'

Simply stating you require an additional single quote character to print a single quote character. That is if you put two single quote characters Oracle will print one. The first one acts like an escape character.

This is the simplest way to print single quotation marks in Oracle. But it will get complex when you have to print a set of quotation marks instead of just one. In this situation the following method works fine. But it requires some more typing labour.

 

Method 2


I like this method personally because it is easy to read and less complex to understand. I append a single quote character either from a variable or use the CHR() function to insert the single quote character.

 

The same example inside PL/SQL I will use like following:

 

DECLARE
  l_single_quote CHAR(1) := '''';
  l_output VARCHAR2(20);
BEGIN
  SELECT 'test single quote'||l_single_quote
  INTO l_output FROM dual;
  DBMS_OUTPUT.PUT_LINE(l_single_quote);
END;

 

The output above is same as the Method 1.

Now my favourite while in SQL is CHR(39) function. This is what I would have used personally:

SELECT 'test single quote'||CHR(39) FROM dual;

The output is same in all the cases.

Now don't ask me any other methods, when I come to know of any other methods I will share here.

Regards,
Anantha

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值