Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to overcome why SQL select cannot find range between tens of thousands and hundreds of thousands

Posted on 2009-04-07
4
Medium Priority
?
274 Views
Last Modified: 2012-05-07
If I run:
SELECT postcode
FROM uk_postcodes
WHERE (x BETWEEN '422813.12' AND '487186.88')
     AND (y BETWEEN '75113.12' AND '139486.88')
I get zero results. Which is not true because If I run:
SELECT x,y
FROM uk_postcodes
WHERE postcode='PO16'
I get this result x = 459,200 y = 106,000
By eye I can see this is in range. So, I changed the last line of my SQL to
      AND (y BETWEEN '100113.12' AND '139486.88')
Notice the low range has changed from 75113.12 to 100113.12 so it is not tens of thousands but hundreds of thousands and then for whatever reason it finds results!!!! How frustrating.

Anyway, is this a known bug? Is there a work-around? Do I just need to format the number somehow so SQL Server 2005 can recognise it?

Many thanks!
0
Comment
Question by:tobzzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
tobzzz earned 0 total points
ID: 24091563
OK, nevermind, I just figured out if I put a 0 before the 75113.12 to make it 075113.12 then MS SQL Server processes my select properly and gives me results. No idea why but it works so I'll just format the number to have 0's in front to match the length of the greater number. Weird!
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24091574
Is x and y numeric? Try this:

SELECT postcode
FROM uk_postcodes
WHERE (x BETWEEN 422813.12 AND 487186.88)
     AND (y BETWEEN 75113.12 AND 139486.88)

It it is not numeric you have to adjust text strings to the same length by '0' prefix.
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24091635
Just to explain - there can be nothing BETWEEN two text strings '75113.12' AND '139486.88' because the first STRING is greater than the second one. How SQL Server should know you would like to compare numbers? Look at implicit data conversions in help.
0
 
LVL 11

Author Comment

by:tobzzz
ID: 24092113
Hi pcelba, thanks for responding. As soon as I noticed adding a 0 to the beginning worked I realised that I was not dealing with a numeric string that should obviously be numberic. So, I converted the field data type to numeric and amended by SQL. It worked perfectly.

Thanks again for your response anyway.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question