Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

query for negative number

Posted on 2009-04-03
8
Medium Priority
?
397 Views
Last Modified: 2013-12-24
I have a form where the user enters in their customer number. The database (SQL 2003) field custno is marked with a negative if that customer is not participating.
If I enter in 1234 how can I query against to see if it matches -1234 in the database?
0
Comment
Question by:usky1
[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
  • 2
  • +2
8 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 24060856
Assuming the column has a numeric data type, just use an equality check with -1234

SELECT  Columns
FROM    YourTable
WHERE  CustomerNumber = -1234

... or using cfqueryparam

<!--- cfsqltype may vary --->
SELECT  Columns
FROM    YourTable
WHERE  CustomerNumber = <cfqueryparam value="-1234" cfsqltype="cf_sql_integer">
0
 
LVL 16

Expert Comment

by:duncancumming
ID: 24061118
or  in a variation on agx's answer:
SELECT  Columns
FROM    YourTable
WHERE  CustomerNumber = <cfqueryparam value="-#Form.CustomerNumber#" cfsqltype="cf_sql_integer">
0
 
LVL 2

Expert Comment

by:albrandwood
ID: 24064815
If you want to see if it exists (irrespective of it's active state)

SELECT Columns
FROM YourTable
WHERE ABS(CustomerNumber)=<cfqueryparam cfsqltype="cf_sql_integer" value="#ABS(Form.CustomerNumber)#">

or if you want to know if it is Active or Inactive, you could do:

SELECT Columns, 1 AS Active
FROM YourTable
WHERE CustomerNumber=<cfqueryparam cfsqltype="cf_sql_integer" value="#Form.CustomerNumber#">
UNION
SELECT Columns, 0 AS Active
FROM YourTable
WHERE CustomerNumber=<cfqueryparam cfsqltype="cf_sql_integer" value="-#Form.CustomerNumber#">
0
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
LVL 52

Expert Comment

by:_agx_
ID: 24064864
> WHERE ABS(CustomerNumber)

Generally, you are better off avoiding using functions on columns unnecessarily. They can hinder the database's usage of indexes, or cause a less efficient use indexes (scan vs. seek).
0
 
LVL 1

Expert Comment

by:Digitalmesh
ID: 24066296
Hi,
    Please try

SELECT  Columns
FROM    YourTable
WHERE  CustomerNumber =  -1 * (#Form.CustomerNumber#)
0
 

Author Comment

by:usky1
ID: 24073915
I'm sure if I'm seeing the solution or maybe I didn't ask the question properly.
If I enter in the customer number and it is 1234 I wnat to make sure the query brings back the proper data. So if I put a negative in the WHERE statement it is alwasy going to look at it as  a negative number.
If I enter in 1234 and that customer is not participating I need to then look at the query value as -1234.
So when I enter in 1234 I need to look at the table column "custno" and check if 1234 is valid and return 1234, if not then check to see if -1234 is valid and if it is return -1234.
1234 is just an example and it can be any 3 - 6 numeric values.
0
 
LVL 2

Accepted Solution

by:
albrandwood earned 1600 total points
ID: 24073949
Your Table
CustomerNumber    Name
1234                  John Doe
-4321                 Jane Doe
2222                      Not Me
3333                      Or You

If you just want to check for ACTIVE customers:
SELECT Columns
FROM YourTable
WHERE CustomerNumber=<cfqueryparam cfsqltype="cf_sql_integer" value="#Form.CustomerNumber#">

form.customernumber = '1234'
Result:
CustomerNumber      Name
1234                   John Doe

If you want to check for INACTIVE customers:
SELECT Columns
FROM YourTable
WHERE CustomerNumber=<cfqueryparam cfsqltype="cf_sql_integer" value="-#Form.CustomerNumber#">

Form.Customernumber=4321
Result:
CustomerNumber      Name
-4321                   Jane Doe

If you want to do it as a single query with a flag to advise you of the Active status, then use a UNION:

SELECT Columns, 1 AS Active
FROM YourTable
WHERE CustomerNumber=<cfqueryparam cfsqltype="cf_sql_integer" value="#Form.CustomerNumber#">
UNION
SELECT Columns, 0 AS Active
FROM YourTable
WHERE CustomerNumber=<cfqueryparam cfsqltype="cf_sql_integer" value="-#Form.CustomerNumber#">

CustomerNumber= 1234
Result:
Customer       Name        Active
1234         John Doe       1
- or -
CustomerNumer=4321
Result:
Customer       Name        Active
-4321        Jane Doe       0
0
 

Author Closing Comment

by:usky1
ID: 31566273
Solution worked great. I even learned something new, thanks.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 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