Summary of Columns in TSQL to return -1 if not equal to 0

Rx_

Nothing In Moderation
Local time
Today, 11:25
Joined
Oct 22, 2009
Messages
2,803
Is there an efficient way to return a -1/0 instead of some number/0?

A table has 4 outer joins to views that return a value or null depending on if that field represents a Federal lease.
These 4 outer joins work.
The result is a ID_Wells column and 4 columns with any combination of values or Null.

This line of TSQL:
ISNULL(ISNULL(dbo.vRE_FedLeaseType.Fed_Lease_Type, '')
+ ISNULL(dbo.vRE_FedLeaseMinOwn.MinOwn, '') + ISNULL(dbo.vRE_FedLeaseSurfOwn.SurfOwn, '') + ISNULL(dbo.vRE_FedDirHzLease.Expr1, ''), '') AS HasFedLease

This creates another final summary column [HasFedLease] that returns 0 or some number.
For the Rule Engine - 0 is False, any other number is True.
Acces links to this SQL View. Is there a quick way with out a UDF to convert numbers > 0 to a -1?

Code:
SELECT     dbo.Wells.ID_Wells, dbo.vRE_FedLeaseType.Fed_Lease_Type, dbo.vRE_FedLeaseMinOwn.MinOwn, dbo.vRE_FedLeaseSurfOwn.SurfOwn, 
                      dbo.vRE_FedDirHzLease.Expr1 AS CountOffedLeaseDirHz, ISNULL(ISNULL(dbo.vRE_FedLeaseType.Fed_Lease_Type, '') 
                      + ISNULL(dbo.vRE_FedLeaseMinOwn.MinOwn, '') + ISNULL(dbo.vRE_FedLeaseSurfOwn.SurfOwn, '') + ISNULL(dbo.vRE_FedDirHzLease.Expr1, ''), '') 
                      AS HasFedLease
FROM         dbo.Wells LEFT OUTER JOIN
                      dbo.vRE_FedDirHzLease ON dbo.Wells.ID_Wells = dbo.vRE_FedDirHzLease.ID_Wells LEFT OUTER JOIN
                      dbo.vRE_FedLeaseSurfOwn ON dbo.Wells.ID_Wells = dbo.vRE_FedLeaseSurfOwn.ID_Wells LEFT OUTER JOIN
                      dbo.vRE_FedLeaseMinOwn ON dbo.Wells.ID_Wells = dbo.vRE_FedLeaseMinOwn.ID_Wells LEFT OUTER JOIN
                      dbo.vRE_FedLeaseType ON dbo.Wells.ID_Wells = dbo.vRE_FedLeaseType.ID_Wells
 
One way would be a CASE statement:

CASE WHEN YourFormula = 0 THEN 0 ELSE -1 END AS AliasName
 
If the numbers are only positive or zero
Code:
 - SIGN([HasFedLease])

This will convert any value other than zero to -1.
Code:
- ABS(SIGN([HasFedLease]))
 
  • Like
Reactions: Rx_
Thanks, will try these out!
 

Users who are viewing this thread

Back
Top Bottom