Solved

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

Posted on 2009-04-07
4
273 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 42

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 42

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Copy Database Wizard 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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

626 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