Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert double quotes into SQL output

Tags:

After I run a query and view the output, for example

select * from People

My output is as follows

First   Last      Email
Ray     Smith     [email protected]

How would I export this data so that it looks as follows?

"Ray","Smith","[email protected]"

Or is there a way to do this within SQL to modify records to contain quotes?

Because when you export, it's going to include the commas anyway, right?

like image 768
Ray Avatar asked Jun 06 '11 14:06

Ray


People also ask

Can you use double quotation marks in SQL?

Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes. That's the primary use anyway.

How do you write a quote in SQL query?

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


2 Answers

If the columns you're interested in are 128 characters or less, you could use the QUOTENAME function. Be careful with this as anything over 128 characters will return NULL.

SELECT QUOTENAME(First, '"'), QUOTENAME(Last, '"'), QUOTENAME(Email, '"')
    FROM People
like image 143
Joe Stefanelli Avatar answered Oct 08 '22 17:10

Joe Stefanelli


select '"'+first+'","'+last+'","'+email+'"'
from people

This is the kind of thing best done in code however, you shouldn't query for presentation.

like image 23
Blindy Avatar answered Oct 08 '22 19:10

Blindy