Solved

SQL - need to search for data/name with single quote

Posted on 2009-03-31
7
317 Views
Last Modified: 2012-05-06
Simple query statement that I am stumbling with...  I have a dealerName column that has records with single quotes as data  

eg.  Vann's

The form bombs without any string cleaning to deal with the single quote.  

I created this to deal with it... but it still returns 'no records found'
function cleanQuote(searchTerm)
	dim preQuote
	dim postQute
	postQuote  = replace(searchTerm,"'","'")
	cleanQuote = postQuote
end function

Open in new window

0
Comment
Question by:jweissdandm
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
apresto earned 105 total points
ID: 24029561
Convert to two single quotes when you save and when you query:
postQuote  = replace(searchTerm,"'","''")
0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24029583
try to search like this:
select * from MyTable where ColumnName = 'ddd''d'

Note that to search for a string with a single quote in MSSQL you should double the single quote ('').

HTH

0
 
LVL 2

Author Comment

by:jweissdandm
ID: 24029707
apresto...

Isn't my code doing the same thing?  If the columnData has a single quote, how does it return the rows if the columnData is replace with 2 single quotes?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 4

Assisted Solution

by:amazingwolf1
amazingwolf1 earned 100 total points
ID: 24029761
in sql server, the T-SQL syntax uses single quote as part of the syntax. to allow you to still use single quotes, the syntax requires to mark a single quote inside a column as a double single quote.

So your replace should be:
replace(searchTerm,"'","''")


0
 
LVL 2

Author Closing Comment

by:jweissdandm
ID: 31564853
Thanks gentlePeople
0
 
LVL 23

Expert Comment

by:apresto
ID: 24030571
Thanks for explaining amazingwolf1:) i was a little busy
Glad we could help
Apresto
0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24036715
Thank you, aspresto - hope we will ALL stay as busy as we can :-)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL: Episode III - Revenge of The Dude 24 52
sql 2014,  lock limit 5 31
convert null in sql server 12 33
Inserting oldest record into new table. 5 22
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question