如何用引号编写SQL语句?

时间:2012-03-12 19:20:52

标签: java sql oracle

  

可能重复:
  In Java, is there a way to write a string literal without having to escape quotes?

我想通过sql查询获得Oracle starttime。我使用这个SQL语句

SQL_Statement = "select to_char(startup_time, 'HH24:MI DD-MON-YY') "Startup time" from  v$instance";

当我尝试使用此SQL语句编写代码时,引号存在问题。编写此SQL语句的正确方法是什么?

祝福

P.S我使用Java。当我尝试运行此SQL查询时:

select  to_char(startup_time, 'HH24:MI DD-MON-YY') 'Startup time' from  v$instance

我收到此错误:

Error starting at line 1 in command:
select  to_char(startup_time, 'HH24:MI DD-MON-YY') 'Startup time' from  v$instance 
Error at Command Line:1 Column:50
Error report:
SQL Error: ORA-00923: FROM keyword not found where expected
00923. 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:

4 个答案:

答案 0 :(得分:5)

在Java中,你需要使用单个' \'来转义字符串中的双引号。字符

SQL_Statement = "select to_char(startup_time, 'HH24:MI DD-MON-YY') \"Startup time\" from  v$instance";

请参阅escaping double quotes in a string in Java

答案 1 :(得分:1)

在所有字符串的双引号(“)内使用单引号(')。

答案 2 :(得分:1)

怎么样:

select q'{This string's got some extra "quotes" in it}' from dual;

可以使用{}或[]或任何漂浮你的船(当然不在你的绳子里)。

答案 3 :(得分:0)

我相信这有助于您区分SQL语句中单引号和双引号的区别

What is the difference between single and double quotes in SQL?