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
Solved

Replace single quote with double when searching for name with apostrophe

Posted on 2013-11-03
7
611 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
  • 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
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 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 500 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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