Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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