472,378 Members | 1,251 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 11466
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
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
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
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
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
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
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
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
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
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
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.