[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Replace single quote with double when searching for name with apostrophe

Posted on 2013-11-03
7
Medium Priority
?
691 Views
Last Modified: 2013-11-03
I have a stored proc and I want to search for names like O'Brian. I tried this and I don't get results back

set @lastname = replace (LTRIM(RTRIM(@lastname)),'''', '')

I think above is replacing it with blank. How can I do this?
0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 39620397
That code will replace double quotes with nothing so not sure that's your problem
set @lastname = replace (LTRIM(RTRIM(@lastname)),'''', '')

What code are you using for the query/
0
 
LVL 7

Author Comment

by:Camillia
ID: 39620407
I've saved the name as O'Brian (with the apostrophe). When searching for the name, I cant replace the apostrophe with blank. I need to replace it with single quotes in stored proc.

This is the where clause if I replace the single quotes with blank. I need to compare it with O'Brian not OBrian


where  opp.ACTIVE = 1   AND  opp.lastname LIKE 'OBrian%' AND  i.BusinessNameId  = 6
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39620414
Hi,

does
where  opp.ACTIVE = 1   AND  opp.lastname LIKE 'O''Brian%' AND  i.BusinessNameId  = 6

work?

Regards
  David
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 35

Expert Comment

by:David Todd
ID: 39620428
Hi,

Look at the quotename function
http://technet.microsoft.com/en-us/library/ms176114.aspx

eg
use ExpertsExchange
go

declare @Parameter varchar( 256 )
set @Parameter = 'O''Brien'

select @Parameter

select quotename( @parameter, '''' )

Results

----------------------
O'Brien

(1 row(s) affected)


----------------------
'O''Brien'

(1 row(s) affected)

HTH
  David
0
 
LVL 7

Author Comment

by:Camillia
ID: 39620442
That link you have is for sql 2012. I have sql 2008

I thought maybe this should work

where  opp.ACTIVE = 1   AND  replace (LTRIM(RTRIM(I.LastName)),'''', '') LIKE 'OBrian%' AND  i.BusinessNameId  = 6

but this didn't bring any rows either.

replace (LTRIM(RTRIM(I.LastName)),'''', '')  still bring O'Brian with apostrophe. That should replace it with blank so I could compare with with OBrian Correct?
0
 
LVL 35

Accepted Solution

by:
David Todd earned 2000 total points
ID: 39620455
Hi,

The link I gave is for sql2012, but you can select the version via a drop down at the top, and I don't believe that this function has changed much over recent versions.

Do check that the other clauses in your where aren't affecting the results while you get O'Brian sorted.

Also what types are you comparing? char and varchar handle trailing spaces differently. I'd have thought that the like 'Something%' wouldn't have needed the rtrim.

The Soundex is weak in SQL - you might need to investigate another function for it. And for performance reasons on searching, I'd think about populating a column with the soundex value.

Is there any chance that you have a case sensitive collation, and the case is muddling things a bit here?

Regards
  David
0
 
LVL 7

Author Comment

by:Camillia
ID: 39620480
let me see. This cant be that hard! will post back
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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