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

x
?
Solved

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

Posted on 2014-02-19
3
Medium Priority
?
370 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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 …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

610 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