473,748 Members | 7,118 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.A dd(@ziplow, SqlDbType.Int, 5).Value = (theZip - 10)
....Parameter.A dd(@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 11611
Anton (an*********@gm ail.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.Ad d(@ziplow, SqlDbType.Int, 5).Value = (theZip - 10)
...Parameter.Ad d(@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(@lowz ip)) AND ltrim(str(@high zip))

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

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

SELECT col1, col2, ... FROM ZIPCODES
WHERE zip_code >= ltrim(str(@lowz ip)) < ltrim(str(@high zip))

Disclaimer: I know nothing about Canadian zip codes.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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*******@eart hlink.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****@sommarsk og.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*********@gm ail.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****@sommarsk og.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
2606
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 identical, just a portion of that text is identical. each table has 5 fields, all different except the blob, which is called "message", so normally I use something like: select * from table1 where message like '%apple%';
4
5779
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 stores other fields from our dynamic forms. The field is called 'FormItemAnswer' and stores text, integer, date, float, etc. Anything the user can type into one of our web forms. The query looks like, select distinct from...
22
3062
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 3, NULL, 9, 82, 25
2
1602
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 the data form a public site. I have not use mysql and php since 2003 I have created the date base with 2 tables 1 for a Jump Menu and on for the content. The contents is in a table called PR and I as soon as I enter the data and hit submit the web...
0
1606
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 are the relevant tables: create table image( imageid integer not null, /* The image's ID */ containerid integer not null, /* The container that owns it */ name varchar(120) not null, /* Its name */ state bigint not null default 0,...
1
2792
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 are the relevant tables: create table image( imageid integer not null, /* The image's ID */ containerid integer not null, /* The container that owns it */ name varchar(120) not null, /* Its name */ state bigint not null default 0,...
6
4846
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 SalesManName AT Alan Time
9
2468
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 through the application in Visual Studio .NET 2003 the application an exception when it executes the query.
3
1894
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 is not something I'm used to seeing in SQL, but the function is to automatically establish what month we are in and how many days there are supposed to be in this month. Then divide the total accumulated values (which are provided in a table) and...
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4593
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.