?
Solved

Having an issue with the MSSQL select statement using the replace function

Posted on 2014-02-19
3
Medium Priority
?
369 Views
Last Modified: 2014-02-19
Hi All,

   I'm trying to do a select statement in MSSQL 2012 and PHP 5.3 from a column filled with telephone numbers.  Some have spaces in between, some have hyphens, some have none.  I tried the following select statement:

select *, replace(dbo.MERCHANT.CONTACT_PHONE, '-', '') as CTPHONE from dbo.MERCHANT where CONTACT_PHONE = '7781234567'

   But it doesn't return any rows.  I've tried inside the SQL Server Management Studio as well, but no rows are returned.

   I've tried googling but I can't seem to find any help there.

Thanks!!
Zack
0
Comment
Question by:IT Tech
[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 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 1000 total points
ID: 39871843
The CONTACT_PHONE still has dashes in it, so your Where clause won't be satisfied.

Try this:

select *, replace(dbo.MERCHANT.CONTACT_PHONE, '-', '') as CTPHONE from dbo.MERCHANT where replace(dbo.MERCHANT.CONTACT_PHONE, '-', '') = '7781234567'
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points
ID: 39871847
>select *, replace(dbo.MERCHANT.CONTACT_PHONE, '-', '') as CTPHONE from dbo.MERCHANT where CONTACT_PHONE = '7781234567'

The SQL query engine processes the SELECT clause of a query dead last, so if you have a row WHERE CONTACT_PHONE='778-123-4567', then you'll have to search for it with all of the dashes/underscore/whatever, regardless of what the replace() does in the SELECT.

Otherwise, if you want to avoid that, then throw it into a subquery.   Something like..

SELECT a.*, a.CTPHONE
FROM (
   select *, replace(dbo.MERCHANT.CONTACT_PHONE, '-', '') as CTPHONE
   from dbo.MERCHANT ) a
where a.CONTACT_PHONE = '7781234567'
0
 

Author Closing Comment

by:IT Tech
ID: 39871880
Thanks very much!!!
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

800 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