Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Query help
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cable_si
Starting Member

20 Posts

Posted - 03/04/2013 :  05:41:16  Show Profile  Reply with Quote
Hi

Can anyone help improving a query i have

I have a table with a list of phone numbers

Table1

Telephone
01212234567
01212234568
01212234569
01212234544
01212234517
01212234527
01212234537
01212234547
01212234557

Iam given 5 phone numbers and i need to check if each one exists in Table1

At the moment i am doing 5 individual querys

set @existsTel1 = 0
select @existsTel1 = 1 from Table1 where Telephone=@telephone1

I would like to do this in one query, can anybody help at all

thanks

Simon

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 03/04/2013 :  05:59:49  Show Profile  Reply with Quote
so is the prefix part always same?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 03/04/2013 :  11:46:34  Show Profile  Reply with Quote
If (SELECT count(1) FROM Table WHERE TelephoneNumbers in ('number1','number2',...,'number5'))=5
SET @existsTel1 = 1
ELSE
SET @existsTel1 = 0

Cheers
MIK
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 03/04/2013 :  23:10:30  Show Profile  Reply with Quote
put those numbers you've in a table and then use a query like

SELECT p.*,
CASE WHEN t.number IS NOT NULL THEN 'Exists' ELSE 'Not EXists' END
FROM PhoneTable p
LEFT JOIN YourTable t
ON p.Telephone LIKE '%' + CAST(t.yournumber AS varchar(5))+ '%'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000