Solved

query for negative number

Posted on 2009-04-03
8
394 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache SSL and mod_rewrite not working 8 247
Use System DSN 6 95
htaccess restrict subdomain 4 147
spamming  on Hosted svrs? 6 109
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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