Jim Metcalf
asked on
query question
I want to query a table in our billing system by account number. I would also like to automatically query account numbers that are numerically 1 higher and 2 higher and numerically 1 lower and 2 lower than the accountnumber in the same query.
for example.
if the accountnumber i want to query is
7778. i would like to automatically query 4 more account numbers also.
7779
7780
7777
7776
the accountnumbers are stored as text. does anyone have an easy way to do this?
thanks a bunch
for example.
if the accountnumber i want to query is
7778. i would like to automatically query 4 more account numbers also.
7779
7780
7777
7776
the accountnumbers are stored as text. does anyone have an easy way to do this?
thanks a bunch
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tried the following using my table and it works
Note that in tbl2, the AcctNos are all text.
declare @accountnumber int--varchar(10);
set @accountnumber = 7778;
select top 10 * from tbl2 where AcctNo between @accountnumber-2 and @accountnumber+2
Note that in tbl2, the AcctNos are all text.
AcctNo AcctName
7776 B
7777 C
7778 D
7779 E
7780 F
ASKER
declare @accountnumber int--varchar(10);
set @accountnumber = 02500500004;
select top 10 * from billing.account where Accountnumber between @accountnumber-2 and @accountnumber+2
ok so i tried this and here is the error i get... do you think it might have to do with the fact
that i have leading zeros
Msg 8115, Level 16, State 2, Line 17
Arithmetic overflow error converting expression to data type int.
Msg 245, Level 16, State 1, Line 18
Conversion failed when converting the nvarchar value '0530433004BAD' to data type int.
set @accountnumber = 02500500004;
select top 10 * from billing.account where Accountnumber between @accountnumber-2 and @accountnumber+2
ok so i tried this and here is the error i get... do you think it might have to do with the fact
that i have leading zeros
Msg 8115, Level 16, State 2, Line 17
Arithmetic overflow error converting expression to data type int.
Msg 245, Level 16, State 1, Line 18
Conversion failed when converting the nvarchar value '0530433004BAD' to data type int.
In '0530433004BAD'
is BAD a part of the account number? You did say they are text but you didn't say they are alphanumeric.
is BAD a part of the account number? You did say they are text but you didn't say they are alphanumeric.
ASKER
Could you please run this to see what you get? It should return zero rows because you mentioned there is no account number like '0530433004BAD'
select * from billing.account where Accountnumber like '%BAD'
If it returns one or more records then we have to rethink the solution.
ASKER
well this is weird
there is one accountnumber that has bad in it 0530433004BAD
there is one accountnumber that has bad in it 0530433004BAD
ASKER
I have to leave for tonight....
thanks for all your help
i think i am closer
thanks for all your help
i think i am closer
Are you going to remove it or we have to go around it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks guys
ASKER
declare @accountnumber varchar(10);
set @accountnumber = '0250050004';
select top 10 * from billing.account where accountnumber between @accountnumber-2 and @accountnumber+2
Msg 245, Level 16, State 1, Line 13
Conversion failed when converting the nvarchar value '0530433004BAD' to data type int.