Solved

query for negative number

Posted on 2009-04-03
8
393 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_
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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