Solved

query for negative number

Posted on 2009-04-03
8
383 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
> 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:Digitalmesh
Comment Utility
Hi,
    Please try

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

Author Comment

by:usky1
Comment Utility
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 400 total points
Comment Utility
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
Comment Utility
Solution worked great. I even learned something new, thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

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

12 Experts available now in Live!

Get 1:1 Help Now