?
Solved

Replace single quote with double when searching for name with apostrophe

Posted on 2013-11-03
7
Medium Priority
?
670 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

762 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