Solved

Replace single quote with double when searching for name with apostrophe

Posted on 2013-11-03
7
577 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 42

Expert Comment

by:Rob Jurd, EE MVE
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now