Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql query with a where clause

Posted on 2015-02-11
7
Medium Priority
?
133 Views
Last Modified: 2015-02-11
I have a problem where I trying to get all people with given phone number. THe problem is the phone numbers are inconsistently stored in the databse. For example,

(111)111-1111,111.111.1111, 111-111-1111

I am using Coldfusion to pull the data., but not sure if I am doing it correctly
 AND (U.cellPhone LIKE <cfqueryparam value="%#ucase(replace(replace(replace(replace(replace(trim(Request.phone),'.','','all'),'-','','all'),'(','','all'),')','','all'),'&nbsp','','all'))#%" cfsqltype="cf_sql_varchar">
			  	  OR U.homePhone LIKE <cfqueryparam value="%#ucase(replace(replace(replace(replace(replace(trim(Request.phone),'.','','all'),'-','','all'),'(','','all'),')','','all'),'&nbsp','','all'))#%" cfsqltype="cf_sql_varchar">
				  OR U.workPhone LIKE <cfqueryparam value="%#ucase(replace(replace(replace(replace(replace(trim(Request.phone),'.','','all'),'-','','all'),'(','','all'),')','','all'),'&nbsp','','all'))#%" cfsqltype="cf_sql_varchar">
				  
				)

Open in new window

0
Comment
Question by:erikTsomik
[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
  • 4
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 total points
ID: 40604248
I would strip out all non-numeric values from the value stored in the database and compare it with the same number-only search criteria.

Use this in coldfusion to reduce the search phone number to digits only:

   <cfset testPhone = rereplace(Request.phone,"[^[:digit:]]*","","all")>

Then compare this number with the database phone number all stripped down to numbers only.   All you need is to find the same type of replace for your particular database.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40604265
but the database have garbage data too i need to normalize the database side so the both sides are the same?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 40604356
Yes, as I said, you need to wrap the column in a replace function just like the CF one to strip out all non-numeric values, then you can compare apples to apples.

something like this:

   select  stuff(workPhone, 1, patindex('%[0-9]%', workPhone)-1, '')

http://stackoverflow.com/questions/18625548/t-sql-select-query-to-remove-non-numeric-characters

https://www.google.com/search?q=sql+server+strip+non-numeric+characters+from+string&ie=utf-8&oe=utf-8
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Author Comment

by:erikTsomik
ID: 40604368
if I do it in the where clause will that be OK .

 OR Replace(Replace(Replace(U.homePhone,'(', ''),')',''),'-','') LIKE '%3013400075%'
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 40604370
sure..
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40604878
>> if I do it in the where clause will that be OK .

Technically there's wrong with it, but it's better for performance if you don't.  Using functions that way will force a table/index scan (essentially the same thing).  Instead, store the unformatted numbers in the db as GD originally suggested.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 40604914
Oh, I thought he meant the where clause of the column, not of the value... agree with agx
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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
What You Need to Know when Searching for a Webhost Provider
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

609 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