Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if phonenumbers exist between 2 columns

I am trying to pass two 10 digit telephone number block using TSQL.

Lets say:

TelephoneNumber1: 1234560095
TelephoneNumber2: 1234561005

My Table consists of 3 columns:

ID, StartBlock, EndBlock
1, 5671231000, 5671232000
2, 1234561000, 1234562000
3, 2175551200, 2175551300

So what I am trying to do is find all the numbers between TelephoneNumber1 and TelephoneNumber2 and see if it exists between any records between StartBlock and EndBlock in the table.

In this example, it would find a number already in use in the second record because when 1234560095 gets to 1234561000, it will find it in already in use.

How do I accomplish this to find a number in use between the StartBlock and EndBlock?

like image 625
Charles Bernardes Avatar asked Jan 15 '13 22:01

Charles Bernardes


1 Answers

declare @T table
(
  ID int,
  StartBlock bigint, 
  EndBlock bigint
)

insert into @T values
(1, 5671231000, 5671232000),
(2, 1234561000, 1234562000),
(3, 2175551200, 2175551300)

declare @TelephoneNumber1 bigint 
declare @TelephoneNumber2 bigint 

set @TelephoneNumber1 = 1234560095
set @TelephoneNumber2 = 1234561005

select * 
from @T
where StartBlock <= @TelephoneNumber2 and 
      EndBlock >= @TelephoneNumber1
like image 186
Mikael Eriksson Avatar answered Oct 06 '22 02:10

Mikael Eriksson