473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with single quotes in SQL statement

Hi all!

I am not very proud to ask this but here is my problem:

string code = "\'13\'"

The string code will have to contain following info:
'51','52','63', 'other'...
to get certain info from the database. When the querry is parsed these
values will be looked up:
....
compCode in (@code)
....

Can someone tell me why these values are not found in the DB? I figured
out that maybe he puts his own quotes so I tried the following:

"51" -->works (but only for one value?)
" \' 51\' " --does not work
"'51'" --does not work

Does anyone have some ideas how I can look up the right values? THX!

DateTime startDate = new DateTime(2006,1 2,14,0,0,0);
DateTime endDate = new DateTime(2006,1 2,15,23,59,59);
string campaignCode = "O850";
string code = "\'13\'"; //,\'51\'
string language = "NL";
string startdate = Convert.ToStrin g(startDate.Yea r + "-" +
startDate.Month + "-" + startDate.Day);
string enddate = Convert.ToStrin g(endDate.Year + "-" +
endDate.Month + "-" + endDate.Day);
// define connection
sqlConn = new SqlConnection(C ONNECTION);

// define query

sqlQuery = "select count(*) as Aantal, sum(talktime) as TalkTime, "
+ "sum(updatetime ) as UpdateTime, sum(talktime + updatetime) as
HandleTime "
+ "from [REPORTSERVERRPT].dbo.SOMETHING"
+ "where calldate @startdate and"
+ " calldate < @enddate and "
+ "compCode in (@code) and "
+ "sleutel in ("
+ "select up.sleutel "
+ "from [REPORTSERVER-RPT].dbo.TEST as up "
+ "where up.CampaignName = @campaign and "
+ "up.lang = @language)";

Dec 15 '06 #1
11 1860
"Elmo" <bv***@concentr a.bewrote in message
news:11******** **************@ n67g2000cwd.goo glegroups.com.. .
Does anyone have some ideas how I can look up the right values? THX!
SELECT * FROM Table WHERE Field IN ('51','52','63' )
Dec 15 '06 #2
That is absolutly not the problem (but thx though ;-))
Maybe following output from sql profiler will help you guys :

Values of my parameters:
INPUT IS :

string code = "13" + "'" + "," +"'" + " 51";

OUPUT IS :
@startDate = '2006-12-14', @endDate = '2006-12-15', @campaign = 'O850',
@language = 'NL', @code = '13'','' 51'

So without asking he puts an extra quote in the string for each single
quote i use

Mark Rae schreef:
"Elmo" <bv***@concentr a.bewrote in message
news:11******** **************@ n67g2000cwd.goo glegroups.com.. .
Does anyone have some ideas how I can look up the right values? THX!

SELECT * FROM Table WHERE Field IN ('51','52','63' )
Dec 15 '06 #3
Ah! I see what you're trying to do. You're trying to do a macro
substitution. You can't do that.

The IN operator is expecting a number of comma delimited parameters (sort
of). You're passing one parameter to it, so it treats it as the first item
in the list - not as a list of many items. I haven't explained that too
well; but I hope you see what I'm getting at.

I'm afraid you'll just have to type in the individual values.

Of course, if you can retrieve the values from a table, you can do a
subselect inside the IN operator's brackets; which could save you some
typing, I suppose.

HTH
Peter

"Elmo" <bv***@concentr a.bewrote in message
news:11******** **************@ n67g2000cwd.goo glegroups.com.. .
Hi all!

I am not very proud to ask this but here is my problem:

string code = "\'13\'"

The string code will have to contain following info:
'51','52','63', 'other'...
to get certain info from the database. When the querry is parsed these
values will be looked up:
...
compCode in (@code)
...

Can someone tell me why these values are not found in the DB? I figured
out that maybe he puts his own quotes so I tried the following:

"51" -->works (but only for one value?)
" \' 51\' " --does not work
"'51'" --does not work

Does anyone have some ideas how I can look up the right values? THX!

DateTime startDate = new DateTime(2006,1 2,14,0,0,0);
DateTime endDate = new DateTime(2006,1 2,15,23,59,59);
string campaignCode = "O850";
string code = "\'13\'"; //,\'51\'
string language = "NL";
string startdate = Convert.ToStrin g(startDate.Yea r + "-" +
startDate.Month + "-" + startDate.Day);
string enddate = Convert.ToStrin g(endDate.Year + "-" +
endDate.Month + "-" + endDate.Day);
// define connection
sqlConn = new SqlConnection(C ONNECTION);

// define query

sqlQuery = "select count(*) as Aantal, sum(talktime) as TalkTime, "
+ "sum(updatetime ) as UpdateTime, sum(talktime + updatetime) as
HandleTime "
+ "from [REPORTSERVERRPT].dbo.SOMETHING"
+ "where calldate @startdate and"
+ " calldate < @enddate and "
+ "compCode in (@code) and "
+ "sleutel in ("
+ "select up.sleutel "
+ "from [REPORTSERVER-RPT].dbo.TEST as up "
+ "where up.CampaignName = @campaign and "
+ "up.lang = @language)";

Dec 15 '06 #4
"Elmo" <bv***@concentr a.bewrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
So without asking he puts an extra quote in the string for each single
quote i use
Ah right - yes, that is standard ADO.NET behaviour for string parameters
because a single quote in T-SQL is the string variable delimter. When T-SQL
encounters a single quote, it interprets it as the beginning of a string
variable containing everything which follows until the next single quote is
reached. To get round the obvious problem of what to do with a string
variable need to contain a single quote, one single quote is converted into
two single quotes.
Dec 15 '06 #5
Peter,

Yup, you understand the problems I'm facing... I also thought of this
extra table you described, but after carefull consideration we
(programmers team) decided against that because these values will vary
(depending on the campaign).

Quote -

so it treats it as the first item
in the list - not as a list of many items. I haven't explained that too
well; but I hope you see what I'm getting at.

I'm afraid you'll just have to type in the individual values.
/Quote

Typing the individual values is the best option but I can't allow users
to change sql statements... and we explicitly decided AGAINST dynamic
SQL :(. I also considered parsing every code (51,60,...) individually
but that is not gonna work because null values can occur (not to
mention the performance issues).

Dec 15 '06 #6
"Elmo" <bv***@concentr a.bewrote in message
news:11******** *************@t 46g2000cwa.goog legroups.com...
Yup, you understand the problems I'm facing... I also thought of this
extra table you described, but after carefull consideration we
(programmers team) decided against that because these values will vary
(depending on the campaign).

Quote -

so it treats it as the first item
>in the list - not as a list of many items. I haven't explained that too
well; but I hope you see what I'm getting at.

I'm afraid you'll just have to type in the individual values.

/Quote

Typing the individual values is the best option but I can't allow users
to change sql statements... and we explicitly decided AGAINST dynamic
SQL :(. I also considered parsing every code (51,60,...) individually
but that is not gonna work because null values can occur (not to
mention the performance issues).
Apologies - I now understand your problem too...

You can get round the problem of creating dynamic SQL client-side, but
you'll have to use a stored procedure instead and create dynamic SQL in
that. Here's an example from AdventureWorks. ..

CREATE PROCEDURE Person.TestProc
@pstrCriteria varchar(100) = NULL
AS

DECLARE @strSQL nvarchar(4000)

SET @strSQL =
'
SELECT
*
FROM
Person.CountryR egion
WHERE
CountryRegionCo de IN (' + REPLACE(@pstrCr iteria, '¬', '''') + ')'
EXEC sp_executesql @strSQL

Then, run the following SQL:
Person.TestProc '¬AD¬,¬AE¬,¬AF¬ '

The trick is to use a very uncommon character (¬, in this case) to pretend
to be the text delimiter...

I fully appreciate that this looks like a complete hacky kludge - and it
is! - but it works, it's extremely fast, it gets round the problem of
client-side SQL Injection, and I haven't found a neater way so far... :-)
Dec 15 '06 #7
''51'',''52'',' '63'',''other'' ...

(doubled single quotes - Transact-SQL considers them escaped)
should work.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Expect the unaccepted.

"Elmo" <bv***@concentr a.bewrote in message
news:11******** **************@ n67g2000cwd.goo glegroups.com.. .
Hi all!

I am not very proud to ask this but here is my problem:

string code = "\'13\'"

The string code will have to contain following info:
'51','52','63', 'other'...
to get certain info from the database. When the querry is parsed these
values will be looked up:
...
compCode in (@code)
...

Can someone tell me why these values are not found in the DB? I figured
out that maybe he puts his own quotes so I tried the following:

"51" -->works (but only for one value?)
" \' 51\' " --does not work
"'51'" --does not work

Does anyone have some ideas how I can look up the right values? THX!

DateTime startDate = new DateTime(2006,1 2,14,0,0,0);
DateTime endDate = new DateTime(2006,1 2,15,23,59,59);
string campaignCode = "O850";
string code = "\'13\'"; //,\'51\'
string language = "NL";
string startdate = Convert.ToStrin g(startDate.Yea r + "-" +
startDate.Month + "-" + startDate.Day);
string enddate = Convert.ToStrin g(endDate.Year + "-" +
endDate.Month + "-" + endDate.Day);
// define connection
sqlConn = new SqlConnection(C ONNECTION);

// define query

sqlQuery = "select count(*) as Aantal, sum(talktime) as TalkTime, "
+ "sum(updatetime ) as UpdateTime, sum(talktime + updatetime) as
HandleTime "
+ "from [REPORTSERVERRPT].dbo.SOMETHING"
+ "where calldate @startdate and"
+ " calldate < @enddate and "
+ "compCode in (@code) and "
+ "sleutel in ("
+ "select up.sleutel "
+ "from [REPORTSERVER-RPT].dbo.TEST as up "
+ "where up.CampaignName = @campaign and "
+ "up.lang = @language)";

Dec 15 '06 #8
"Kevin Spencer" <sp**@uce.govwr ote in message
news:u6******** ******@TK2MSFTN GP03.phx.gbl...
''51'',''52'',' '63'',''other'' ...

(doubled single quotes - Transact-SQL considers them escaped)
should work.
Should, but shouldn't... at least, not in the context that the OP wishes to
use them...
Dec 15 '06 #9
"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:uQ******** *****@TK2MSFTNG P02.phx.gbl...
Should, but shouldn't...
Sorry, I meant to say "Should, but doesn't..."
Dec 15 '06 #10

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

Similar topics

12
28572
by: Mosher | last post by:
Hi all, I have an issue with php and/or mysql. I have a php form that writes "items" to a mysql database, including a description of the item. On the mysql server, "magic_quotes_gpc" is ON. I am testing it now by putting special characters in the description field, this is what I am entering: O'Leary "special edition"
5
2730
by: Drifter | last post by:
The quote below is part of the information i want to save to a MS Access database - or update, as the case may be. The original database was built a long time ago, and looking at the code for add/update on the original site-it does not compensate for odd syntax like the quotes. Problem i am hitting now is syntax issue in the SQL when I go to save/update. The syntax present causes failures every time. User said she had no problems...
6
2568
by: Allan | last post by:
Please help, below is my problem. Let's say I have 2 tables, a Products table and a Colors table that go as follow: Table Products prodID Name 1 shirt 2 tshirt
5
11448
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. Queston: How do you handle a string that contains both single & double quotes (i.e. 12'X7") Here's the function:
5
1730
by: Tim::.. | last post by:
Can someone tell me how I convert this simple SQL statement so I can use it in ASP.NET??? I have an issue with the quotation marks and wondered if there is a simple rule for converting the sql statement so if can be used in ASP.NEt! Thanks ... SQL String
15
5257
by: abracad_1999 | last post by:
I am trying to populate a table with the following insert query run through phpmyadmin. When I attempt to run it phpmyadmin just freezes. After a while "Fatal error: Maximum execution time of 300 seconds exceeded in /web/myadmin/283/libraries/import.lib.php on line 19" appears but no indication as why the command fails. Any advice most welcome. INSERT INTO `countries` ( `id` , `country` ) VALUES ('1', 'Abkhazia'), ('2',...
5
1436
by: andy.z | last post by:
I'm writing a PHP line to the foot of a file using another language. my problem is I'm not sure how to write it so that the quotes (both single and double) are corret for PHP to process. The language I'm writing the PHP line with uses single quotes for a print statement. Therefore the line I want to write is (reducing to a single array element to simplify):
9
1689
by: Dave | last post by:
Hi guys, I have just set up a duplicate server running: apache 2.54, mysql 5.04 and php 5.04 This is the same setup as as the server we are using now, apart from the hardware inside. I have copied across the database and website, with exact same permissions as the first server. The problem is that part of the php code is executing but others
30
2800
by: Einstein30000 | last post by:
Hi, in one of my php-scripts is the following query (with an already open db-connection): $q = "INSERT INTO main (name, img, descr, from, size, format, cat, host, link, date) VALUES ('$name', '$img', '$descr', '$user', '$size', '$format', '$cat', '$host', '$link', '$date')" or die(mysql_error()); And when the query gets executed i get back the following error:
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.