Solved

Query  - show only records where a field has a numeric value AND the value is > 6

Posted on 2015-02-02
3
84 Views
Last Modified: 2015-02-02
I have a  field that can contain number or letters.  

I want to return records if the field contains a number AND the number is > 6

What would be the syntax?
0
Comment
Question by:vbnetcoder
3 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 40584874
select * from yourtable
where isnumeric(yourfield) != 0
and yourfield > 6
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40584877
SELECT ...
FROM table_name
WHERE
    1 =
        CASE WHEN column_name LIKE '%[^0-9]%' THEN 0
                  WHEN column_name > 6 THEN 1
                  ELSE 0 END

You have two potential query-breaking issues here:
1) isnumeric() is unreliable for only ints because it has to check so many possible numeric formats:
SELECT ISNUMERIC('10E0'),ISNUMERIC('2,3444'),ISNUMERIC('4D1'),ISNUMERIC('$')

2) SQL can "re-arrange" conditions so unless you use CASE to force a specific order of comparison, SQL could get a non-numeric comparison even though you checked for numeric.

For example, even though you wrote:
WHERE column_name <matches the number format I want> and
    column_name > 6
SQL might treat it as:
WHERE (column_name > 6)  AND ....
and non-numeric values in column_name would cause an abend.
0
 

Author Closing Comment

by:vbnetcoder
ID: 40584910
ty
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

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.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now