[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-03-31
7
Medium Priority
?
326 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 420 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Assisted Solution

by:amazingwolf1
amazingwolf1 earned 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

872 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