Solved

sql query with a where clause

Posted on 2015-02-11
7
126 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
  • 4
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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