Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2006, 02:24 AM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Syntax

Hi! Ihave a problem in my SQL Syntax,

suppose i will search for a name which includes apostrophe (')
for ex:

SELECT * FROM tblCompany WHERE COMPNAME = 'ALYSONS' MERCHANDISE'

this will prompt an error:
     Server: Msg 170, Level 15, State 1, Line 1
     Line 1: Incorrect syntax near 'MERCHANDISE'.
     Server: Msg 105, Level 15, State 1, Line 1
     Unclosed quotation mark before the character string '
     '.

how can i search for a name with an apostrophe on it?
please help....
thank you!!!!



 
Old February 1st, 2006, 05:33 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default


Hi yves,

Use this,

SELECT * FROM tblCompany WHERE COMPNAME = 'ALYSONS'+''''+'MERCHANDISE'


nalaka hewage
 
Old February 1st, 2006, 09:31 AM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One way
SELECT * FROM tblCompany WHERE COMPNAME = 'ALYSONS'' MERCHANDISE'

just double the single quote

Test it out

create table #test (COMPNAME varchar(50))
insert into #test
select 'ALYSONS'' MERCHANDISE'

select * from #test


SELECT * FROM #test WHERE COMPNAME = 'ALYSONS'' MERCHANDISE'

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old February 2nd, 2006, 02:25 AM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for your replies... but the scenario is this:
suppose i have a variable containing the name:

/* ASSUMING THE USER INPUTS: ALYSONS' STORE*/
/* ACTUALLY INPUTS WILL BE PASSED TO A VARIABLE SO I HAVE NO CONTROL
   OF DOUBLING THE QUOTE, I AM ACTUALLY USING VB 6 AS FRONT END. */


s = "SELECT * FROM tblCompany WHERE COMPNAME = '" & txtCompName & "'"

/* where s is actually:
  SELECT * FROM tblCompany WHERE COMPNAME = 'ALYSONS' STORE'
*/

/*so when i call this query:*/

set rs = cn.execute (s)

/* forsure an error will come out*/

I HOPE YOU WILL STILL FIND TIME TO GIVE ME SOME SUGGESTIONS AND IDEAS WITH THIS... THANKS A LOT.


 
Old February 2nd, 2006, 04:13 AM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you aren't using the quoted identifiers option then use double quotes..

s = "SELECT * FROM tblCompany WHERE COMPNAME = """ & txtCompName & """"
or
s = "SELECT * FROM tblCompany WHERE COMPNAME = \"" & txtCompName & "\""
Should give you
SELECT * FROM tblCompany WHERE COMPNAME = "ALYSONS' STORE"

Another option would be:
Create a function to parse through the txtCompName variable looking for single quotes,
if a single quote is found then replace single quote with two single quotes.

"ALYSONS' STORE" would go into the function and "ALYSONS'' STORE" would be returned.
 
Old February 2nd, 2006, 08:14 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or you could create a function i VB that takes a string and replaces every occurance of ' with ''
.. and then use that in place of your variabel.

Gert

 
Old February 2nd, 2006, 08:26 PM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alright!!!
Thank you so much guys!!!
These really helps... Thanks!!!!
God bless!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with SQL syntax DeannaF829 VB Databases Basics 2 April 30th, 2007 12:12 PM
SQL-syntax grstad SQL Language 9 January 30th, 2007 02:40 PM
syntax of sum in SQL heghtera Access VBA 1 March 10th, 2006 03:17 AM
SQL Syntax Cinderella Classic ASP Basics 3 July 21st, 2004 01:06 PM
SQL Syntax jeffg22 SQL Language 6 July 28th, 2003 06:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.