473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

quotation marks in sql statement

Hi !

In my sql statement, i have to use field names and table name with quotation
marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object, it
gets an error ... because of backslashes.

How can i use quotation marks without backslash in a sql statement ?

I have been stuck on this, any help will be appreciated.

Thank you,

Adam

Dec 5 '07 #1
12 3557
On Wed, 05 Dec 2007 14:05:02 -0800, Adam Right <n/a@n/a.comwrote:
In my sql statement, i have to use field names and table name with
quotation
marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object,it
gets an error ... because of backslashes.
What backslashes? If you have properly constructed the string, no
backslashes would actually be present. The backslash-quoting is handled
by the compiler, and by the time the string is assigned to the variable,
no backslashes would remain.

If you get an error specifically complaining about backslashes (is it
actually true that you are?), then you didn't construct the string
correctly.

Of course, there is the additional issue that you didn't actually post the
code you're using. How do I know? The code you posted won't even
compile, since you're missing a backslash. Since we know you didn't just
copy-and-paste the code causing a problem, we also know that all sorts of
things could be missing from the example, including whatever it is that's
actually causing the problem. :(
How can i use quotation marks without backslash in a sql statement ?
Normally, you would do it just as you've tried to do in the example you
posted. So for your question to be answered, you need to do at least two
things:

1) post the _exact_ code you are using
2) describe the _exact_ error you are getting (in particular, unless
the error specifically says something about the backslashes, you have no
reason to believe that the backslashes are definitely the problem)

Pete
Dec 5 '07 #2

"Adam Right" <n/a@n/a.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi !

In my sql statement, i have to use field names and table name with
quotation marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object, it
gets an error ... because of backslashes.

How can i use quotation marks without backslash in a sql statement ?
The other options are:

Use char(34), 34 being the ASCII code for the quote character:

char dblquote = 34;
string sqlStr = "SELECT " + dblquote + "Person Name" + dblquote + ......

Use a verbatim string literal:

string sqlStr = "SELECT ""Person Name"", ""Age Field"" FROM ""Old Table"";";

>
I have been stuck on this, any help will be appreciated.

Thank you,

Adam



Dec 5 '07 #3
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
Use a verbatim string literal:

string sqlStr = "SELECT ""Person Name"", ""Age Field"" FROM ""Old Table"";";
Slight typo correction (I'm absolutely sure Ben knows how to use a
verbatim string literal :) - it needs the @ prefix:

string sqlStr =
@"SELECT ""Person Name"", ""Age Field"" FROM ""Old Table"";";
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 5 '07 #4

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Adam Right" <n/a@n/a.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hi !

In my sql statement, i have to use field names and table name with
quotation marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object, it
gets an error ... because of backslashes.

How can i use quotation marks without backslash in a sql statement ?

The other options are:

Use char(34), 34 being the ASCII code for the quote character:

char dblquote = 34;
string sqlStr = "SELECT " + dblquote + "Person Name" + dblquote + ......

Use a verbatim string literal:

string sqlStr = "SELECT ""Person Name"", ""Age Field"" FROM ""Old
Table"";";
and of course I forgot the all-important @.

string sqlStr = @"SELECT ""Person Name"", ""Age Field"" FROM ""Old
Table"";";
>
>>
I have been stuck on this, any help will be appreciated.

Thank you,

Adam




Dec 5 '07 #5
You might find that

string sql = @"SELECT [Person Name], [Age Field] FROM [Old Table];";

resolves the problems: (?) depends on the SQL dialect of your data source.
Dec 6 '07 #6
AA2e72E <AA*****@discussions.microsoft.comwrote:
You might find that

string sql = @"SELECT [Person Name], [Age Field] FROM [Old Table];";

resolves the problems: (?) depends on the SQL dialect of your data source.
If it does, the @ is unnecessary, as there are no backslashes in the
string anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 6 '07 #7
On Thu, 06 Dec 2007 00:09:22 -0800, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
AA2e72E <AA*****@discussions.microsoft.comwrote:
>You might find that

string sql = @"SELECT [Person Name], [Age Field] FROM [Old Table];";

resolves the problems: (?) depends on the SQL dialect of your data
source.

If it does, the @ is unnecessary, as there are no backslashes in the
string anyway.
True. But there weren't any backslashes in the originally posted string
either (even though there were in the literal in the code, the string
itself wouldn't have had any).

I wish I knew more about SQL (hardly use it myself), because I find myself
thinking crazy things like "maybe the field and table names themselves
have double-quotes in them" and "maybe his problem is that he _needs_ the
backslash characters in the SQL string, with the code he's writing failing
to put them there".

Just goes to show: writing a good question isn't trivial, but it is very
important in getting an answer. So far I'd say the question hasn't been
presented very well.

Pete
Dec 6 '07 #8
Peter Duniho <Np*********@nnowslpianmk.comwrote:
If it does, the @ is unnecessary, as there are no backslashes in the
string anyway.

True. But there weren't any backslashes in the originally posted string
either (even though there were in the literal in the code, the string
itself wouldn't have had any).
Indeed. I suspect actually this is another case of the OP looking at
the string in the debugger, seeing the backslashes in the Locals
window, and thinking that the string itself contains backslashes when
it doesn't.
I wish I knew more about SQL (hardly use it myself), because I find myself
thinking crazy things like "maybe the field and table names themselves
have double-quotes in them" and "maybe his problem is that he _needs_ the
backslash characters in the SQL string, with the code he's writing failing
to put them there".
Yes, I know the feeling. I know the "happy path" SQL where there aren't
extra issues like encoding etc, but not depths of it.
Just goes to show: writing a good question isn't trivial, but it is very
important in getting an answer. So far I'd say the question hasn't been
presented very well.
Perhaps. I suspect the OP is just confused about what the debugger's
telling him, really.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 6 '07 #9
On 5 Dec, 22:05, "Adam Right" <n/a@n/a.comwrote:
Hi !

In my sql statement, i have to use field names and table name with quotation
marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object, it
gets an error ... because of backslashes.

How can i use quotation marks without backslash in a sql statement ?

I have been stuck on this, any help will be appreciated.

Thank you,

Adam
Can you do a DESC on the table in question and post that. I'm
suspicious of your sql above. Did someone really name all the fields
"something Field" and the tables "something Table"???
In general you should NOT use spaces in field or table names. Either
do it LikeThis or Like_This. That way you don't have to worry about
this sort of issue.
Dec 6 '07 #10
In general you should NOT use spaces in field or table names. Either
do it LikeThis or Like_This. That way you don't have to worry about
this sort of issue.
In postgresql, and probably some others, you need quoting not only for
spaces, but also to use uppercase letters.
Dec 6 '07 #11
From: Ben Voigt [C++ MVP] [mailto:rb*@nospam.nospam]
Posted At: Friday, 7 December 2007 8:38 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: quotation marks in sql statement
Subject: Re: quotation marks in sql statement
In general you should NOT use spaces in field or table names. Either
do it LikeThis or Like_This. That way you don't have to worry about
this sort of issue.

In postgresql, and probably some others, you need quoting not only for
spaces, but also to use uppercase letters.

Postgresql needs quoting for uppercase letters? That seems pretty
primitive.

I would have to agree with DeveloperX on this one, never use spaces in
your table or field names, and especially never use double quotes in
them (I've seen it done).

Regards,
Wingot

Dec 7 '07 #12

"Wingot" <wi****@newsgroup.nospamwrote in message
news:00****************************@CVW.local...
>From: Ben Voigt [C++ MVP] [mailto:rb*@nospam.nospam]
Posted At: Friday, 7 December 2007 8:38 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: quotation marks in sql statement
Subject: Re: quotation marks in sql statement
In general you should NOT use spaces in field or table names. Either
do it LikeThis or Like_This. That way you don't have to worry about
this sort of issue.

In postgresql, and probably some others, you need quoting not only for
spaces, but also to use uppercase letters.


Postgresql needs quoting for uppercase letters? That seems pretty
primitive.
Maybe I didn't explain that clearly enough -- without quoting postgresql
converts all identifiers to lower case, as a way of being case-insensitive.
As a result, you cannot match a field name that isn't lowercase without
quotes. Of course, the field name would have been converted to lowercase at
creation unless quotes were originally used.

I find that it's "a good thing" to quote all references to database objects
anyway, to distinguish them from keywords and built-in functions.
>
I would have to agree with DeveloperX on this one, never use spaces in
your table or field names, and especially never use double quotes in
them (I've seen it done).

Regards,
Wingot

Dec 11 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NikkoW | last post by:
I need to assign a string to a variable but the text string includes quotation marks: Example: MyString = "He turned and said "Hello" before leaving." When the compiler hits the quotation...
8
by: Stephen Poley | last post by:
One disadvantage of using a simple text editor to produce HTML is that it is relatively time-consuming to put in the proper typographical quotation marks and dashes. A round tuit having arrived...
63
by: Tristan Miller | last post by:
Greetings. Do any popular browsers correctly support <q>, at least for Western languages? I've noticed that Mozilla uses the standard English double-quote character, ", regardless of the lang...
4
by: Thomas Miskiewicz | last post by:
Hi! Is using of a double quotation mark with a URL a problem? For example: http://myserver.com/query?field1=something&field2=test&params="field1=test1"+"field2=test2" Regards Thomas
7
by: Paradigm | last post by:
I am trying to create a recordset where some text fields are matching. The problem is that some of the text fields contain quotation marks. I have tried to create the sql string using replace eg....
2
by: Dixie | last post by:
Hi, I am tyring to write some generic code that will send the source SQL for a mailmerge to a Word template. I am trying to use DLookup to insert the query name that is the record source for the...
3
by: Ufit | last post by:
Simple,dumm question - how to include quotation marks in the string? F.ex. "Data Source=.\SQLEXPRESS;AttachDbFilename="C:\client data.mdf";Integrated Security=True;User Instance=True" I get syntax...
1
by: nonni | last post by:
Hi. I came across this site while looking for a function to replace Quotation marks in SQL. This is my code. <!-- Begin Code --> Intro = Replace(Intro,"'","''") Text =...
31
by: The Bicycling Guitarist | last post by:
Hi. For many years I have been using &quot; for double quotation marks in the HTML code, but the opening and closing quotation marks render the same in my browser. I'm considering going through and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.