?
Solved

sql query with a where clause

Posted on 2015-02-11
7
Medium Priority
?
131 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

ATEN's HDBaseT Presentation at InfoComm 2017

Hear ATEN Product Manager YT Liang review HDBaseT technology, highlighting ATEN’s latest solutions as they relate to real-world applications during her presentation at the HDBaseT booth at InfoComm 2017.

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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