Solved

query for negative number

Posted on 2009-04-03
8
386 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
website does not load without www 12 70
Forbidden errors 5 121
wordpress limitations 4 104
Having trouble setting Apache to recognise 2 vitual sites (on different drive). 3 57
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

23 Experts available now in Live!

Get 1:1 Help Now