473,511 Members | 16,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Query to Select VarChar Rows that can convert to Integer

I have a table of zip codes, some with Canadian zips. I'd like to take
a zip code and search for nearby zips. For example:

Dim theZip As Integer = textbox1.text

....Parameter.Add(@ziplow, SqlDbType.Int, 5).Value = (theZip - 10)
....Parameter.Add(@ziphigh, SqlDbType.Int, 5).Value = (theZip + 10)

SELECT * from ZIPCODES where Cast(zip_code as Integer) BETWEEN @lowzip
AND @highzip

Problem is the letters in the Canadian records cannot be cast as
integers for this process. I get this error:

Syntax error converting the varchar value '53151 1' to a column of data
type int.

Is there a SQL query that can exclude if no cast can be made to an
Integer?

Thanks!

Anton

Jul 23 '05 #1
5 11574
Anton (an*********@gmail.com) writes:
I have a table of zip codes, some with Canadian zips. I'd like to take
a zip code and search for nearby zips. For example:

Dim theZip As Integer = textbox1.text

...Parameter.Add(@ziplow, SqlDbType.Int, 5).Value = (theZip - 10)
...Parameter.Add(@ziphigh, SqlDbType.Int, 5).Value = (theZip + 10)

SELECT * from ZIPCODES where Cast(zip_code as Integer) BETWEEN @lowzip
AND @highzip

Problem is the letters in the Canadian records cannot be cast as
integers for this process. I get this error:

Syntax error converting the varchar value '53151 1' to a column of data
type int.

Is there a SQL query that can exclude if no cast can be made to an
Integer?


Not really. You can add a condition like:

AND zip NOT LIKE '%[^0-9]%'

to specify that you only want numeric values. However, you cannot instruct
the optimizer to check this condition before anything else.

In any case, I would assume that you want these non-digit zip codes as
well. So why not this:

SELECT col1, col2, ... FROM ZIPCODES
WHERE zip_code BETWEEN ltrim(str(@lowzip)) AND ltrim(str(@highzip))

although this is a little devilish. Say that the user specified 53141.
Should "53151 1" be included or not? Maybe this is better:

...Parameter.Add(@ziphigh, SqlDbType.Int, 5).Value = (theZip + 11)

SELECT col1, col2, ... FROM ZIPCODES
WHERE zip_code >= ltrim(str(@lowzip)) < ltrim(str(@highzip))

Disclaimer: I know nothing about Canadian zip codes.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
The call it a "Postal Code" in Canada, not a ZIP code. And they are
logically and legally different things, so they ought to be in separate
tables.

ZIP codes are CHAR(5), or CHAR(9) for ZIP+4, not numerics. Programmers
coming to SQL from BASIC still think that they are dealing with a
interpreted language.

Finally, you are doomed anyway. To find physically contigous ZIP
codes, math will not work; you need a look up table or to add
(longtitude, latitude) pairs to your data. You need to Google Melissa
Data and get some of their mailing list software.

CREATE USAMailings
(..
zip_code CHAR(5) NOT NULL
CHECK (zip_code LIKE '[0-9][0-9][0-9][0-9][0-9]',

);

Remember to use French characters and British spelling checks!!
CREATE CANMailings
(..
postal_code CHAR(7) NOT NULL
CHECK (postal_code LIKE '[A-Z][0-9][A-Z]-[0-9][A-Z][0-9]',
..
);

Jul 23 '05 #3
--CELKO-- (jc*******@earthlink.net) writes:
Finally, you are doomed anyway. To find physically contigous ZIP
codes, math will not work; you need a look up table or to add
(longtitude, latitude) pairs to your data. You need to Google Melissa
Data and get some of their mailing list software.


How true. I recently was out one night and met with two friends, and one
complained that I used the wrong zip code (or "postnummer" as well call
it over here) went I sent him post cards. I had used 171 38, when the
correct code was 169 35. I had the same zip code listed for the other
guy, so I asked that too was wrong. No, his zip code was really 171 38.

They live across the street from each other.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Celko:

Good advice, but I'm limited to an XML web service feed. Anyway, this
got me closer:

"SELECT * FROM <table> where (isNumeric(zip_code) = 1) and
(Cast(zip_code as Integer) BETWEEN Cast(@lowzip as Integer) AND
Cast(@highzip as Integer))

Needless to say, once it started working it confirmed your doomsday
premonition - math will not work to find contigous ZIP codes.

I'll need to relate this query to another look up table. Thanks for the
feedback.

Anton

Jul 23 '05 #5
Anton (an*********@gmail.com) writes:
Good advice, but I'm limited to an XML web service feed. Anyway, this
got me closer:

"SELECT * FROM <table> where (isNumeric(zip_code) = 1) and
(Cast(zip_code as Integer) BETWEEN Cast(@lowzip as Integer) AND
Cast(@highzip as Integer))


It appears that you have already realised that this query is dead for
other reasons, but permit me to point out a couple of techcnial
problems with it:

1) There is no guarantee on evaluation order, so you could still get a
conversion error.
2) isnumeric is virtually useless. It returns 1 if the string can be
converted to any numeric data type, but just because the string can
be converted to float or money, does not mean that it can convert to
integer. The expression NOT LIKE '%(^0-9]%' is good for finding
positive integers.
3) When you place a column in an expression live above, this means that
any index on that column will not be useful. (In this case: the index
would be sorted in string order and not numeric order.)


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2587
by: Beyonder | last post by:
I have five tables in my database, there are actually NO common fields between them, not even a KEY or ID or anything like that, except for the "body" of a blob field. and that text is not...
4
5765
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also...
22
3007
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
2
1596
by: Jeffrey D Moncrieff | last post by:
I am trying to create a data base for colleting date for my research project. I have run in to a couple of problems and I need it fast ASAP I need it to be able to add data and edit them and view...
0
1589
by: Robert Wille | last post by:
I have a number of very common queries that the optimizer plans a very inefficient plan for. I am using postgres 7.2.3. I vacuum hourly. I'm wonderingwhat I can do to make the queries faster. Here...
1
2755
by: Robert Wille | last post by:
I have a number of very common queries that the optimizer plans a very inefficient plan for. I am using postgres 7.2.3. I vacuum hourly. I'm wonderingwhat I can do to make the queries faster. Here...
6
4816
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
9
2446
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
3
1867
by: KishenGajjar | last post by:
I'm trying to convert a query that runs in Query Analyser (SQL Server 2000) to work in MS Access. This will then help me modify the query further and use it to create reports and such. This query...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.