2

Possible Duplicate:
In Java, is there a way to write a string literal without having to escape quotes?

I want to get Oracle starttime with sql query. I use this SQL statement

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

There is a problem with the quotes when I try to write a code with this SQL statement. What is the proper way to write this SQL statement?

Best wishes

P.S I use Java. When I try to run this SQL query:

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

I get this error:

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:
Community
  • 1
  • 1
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • try replacing the double quotes inside the string with single quotes. you're unintentionally closing your SQL query string. – Eliran Malka Mar 12 '12 at 19:24
  • plus, please show more research effort next time.. a simple search on google or stackoverflow would've flooded your browser with this same answer. – Eliran Malka Mar 12 '12 at 19:26
  • @user1103606 - What language are you using to interact with the database? That language will have some way to escape double quotes inside a string. My guess is that using a single `\` before the embedded double quotes will work but that's a guess without knowing the language you're using. – Justin Cave Mar 12 '12 at 19:27

4 Answers4

5

In Java, you need to escape the double quotes inside the string using a single '\' character

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

See escaping double quotes in a string in Java

Community
  • 1
  • 1
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
1

Use single quotes (') when inside double quotes (") for all strings.

Joel
  • 1,309
  • 2
  • 10
  • 20
1

How about:

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

Can use {} or [] or whatever floats your boat (thats not in your string of course).

tbone
  • 15,107
  • 3
  • 33
  • 40
0

I believe this will help you distinguish between the differences of the single and double quotations within SQL statements

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

Community
  • 1
  • 1
mW00t
  • 199
  • 2
  • 9