If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > double quote or single quote?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-22-07, 03:18
Panoy Panoy is offline
Registered User
 
Join Date: Mar 2007
Posts: 77
Talking double quote or single quote?

Hi to all,

I want to ask your opinions about what kind of quote to use in appending or using string values in our SQL statements. Is it the single quote or the double quote? For me, I think the double quote is very effective because, it can accept a string value as a whole, even if the string value has a single quote in it, (ex: Toby's), in which we could write it as "Toby's".

How about the single quote? It generates error if use would replace those double quotes in the example string above, because the string itself has a single quote in it. Putting escape characters would do the trick but isn't it a little hard to track if you are writing a lot/longer SQL statements?

Maybe there are things that I did not know about? I see many examples in books, using the single quote. Even the dump file of mySQL uses single quotes (correct me if I am wrong)?

Happy to hear from you guys!
Thanks and god bless all!
Reply With Quote
  #2 (permalink)  
Old 06-22-07, 05:54
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
if you use double quotes then
Code:
select "Toby's ok"       -- will work
select " "Hello" said Peter"   -- will fail
if you use single quotes all the time
Code:
select 'Toby's ok'       -- will now fail
select ' "Hello" said John'   -- will now work
You can either swap from single to double quotes depending on what's in your string but this can look messy. I prefer to use single quotes because when you need to embed your sql in PHP you'll be able to use double quotes in your PHP code to surround the SQL and this in turn allows you to use PHP variables in your SQL. You can also just simply remove the quotes to make life easier

Code:
$name = "John";
$sql = "select 'Hello said $name' ";
You will of course need to escape any use of the same quotes inside your string whatever though. Quotes are always a minefield and it's best to build up your queries slowly. It gets even more fun in unix shell where backwards quotes run what ever is within them and return the output as the string.

Mike
Reply With Quote
  #3 (permalink)  
Old 06-22-07, 06:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
double quotes work only in mysql, if you should ever have a need to port your database to some other system, your queries will fail

single quotes are standard sql, whereas double quotes in standard sql are used to delimit table and column names containing special characters

furthermore, standard sql use two consecutive single quotes to escape a single quote inside a string --
Code:
'o''toole'
mysql supports this use of standard sql (always has), even though it allows other escape characters as well
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On