You can use a CASE statement.
SELECT
CASE WHEN currate.currentrate IS NULL THEN 1 ELSE currate.currentrate END
FROM ...
Answer from Justin Helgerson on Stack OverflowYou can use a CASE statement.
SELECT
CASE WHEN currate.currentrate IS NULL THEN 1 ELSE currate.currentrate END
FROM ...
You can use COALESCE:
SELECT orderhed.ordernum,
orderhed.orderdate,
currrate.currencycode,
coalesce(currrate.currentrate, 1) as currentrate
FROM orderhed
LEFT OUTER JOIN currrate
ON orderhed.company = currrate.company
AND orderhed.orderdate = currrate.effectivedate
Or even IsNull():
SELECT orderhed.ordernum,
orderhed.orderdate,
currrate.currencycode,
IsNull(currrate.currentrate, 1) as currentrate
FROM orderhed
LEFT OUTER JOIN currrate
ON orderhed.company = currrate.company
AND orderhed.orderdate = currrate.effectivedate
Here is an article to help decide between COALESCE and IsNull:
http://www.mssqltips.com/sqlservertip/2689/deciding-between-coalesce-and-isnull-in-sql-server/
When you want to replace a possibly null column with something else, use IsNull.
SELECT ISNULL(myColumn, 0 ) FROM myTable
This will put a 0 in myColumn if it is null in the first place.
You can use both of these methods but there are differences:
SELECT ISNULL(col1, 0 ) FROM table1
SELECT COALESCE(col1, 0 ) FROM table1
Comparing COALESCE() and ISNULL():
The ISNULL function and the COALESCE expression have a similar purpose but can behave differently.
Because ISNULL is a function, it is evaluated only once. As described above, the input values for the COALESCE expression can be evaluated multiple times.
Data type determination of the resulting expression is different. ISNULL uses the data type of the first parameter, COALESCE follows the CASE expression rules and returns the data type of value with the highest precedence.
The NULLability of the result expression is different for ISNULL and COALESCE. The ISNULL return value is always considered NOT NULLable (assuming the return value is a non-nullable one) whereas COALESCE with non-null parameters is considered to be NULL. So the expressions ISNULL(NULL, 1) and COALESCE(NULL, 1) although equivalent have different nullability values. This makes a difference if you are using these expressions in computed columns, creating key constraints or making the return value of a scalar UDF deterministic so that it can be indexed as shown in the following example.
-- This statement fails because the PRIMARY KEY cannot accept NULL values -- and the nullability of the COALESCE expression for col2 -- evaluates to NULL.
CREATE TABLE #Demo
(
col1 integer NULL,
col2 AS COALESCE(col1, 0) PRIMARY KEY,
col3 AS ISNULL(col1, 0)
);
-- This statement succeeds because the nullability of the -- ISNULL function evaluates AS NOT NULL.
CREATE TABLE #Demo
(
col1 integer NULL,
col2 AS COALESCE(col1, 0),
col3 AS ISNULL(col1, 0) PRIMARY KEY
);
Validations for ISNULL and COALESCE are also different. For example, a NULL value for ISNULL is converted to int whereas for COALESCE, you must provide a data type.
ISNULL takes only 2 parameters whereas COALESCE takes a variable number of parameters.
if you need to know more here is the full document from msdn.
You can use NVL:
NVL(col, 0)
or COALESCE:
COALESCE(col, 0)
You can use either NVL() or COALESCE().
SELECT NVL(NULL, 0) from dual;
SELECT COALESCE(NULL, 0) from dual;
NVL will return either the non-null value or, if it is null then the second parameter.
COALESCE allows for multiple parameters and will evaluate to the first non-null value in the parameter list.
Instead of COALESCE(a.addressid,0) AS addressexists, use CASE:
CASE WHEN a.addressid IS NOT NULL
THEN 1
ELSE 0
END AS addressexists
or the simpler:
(a.addressid IS NOT NULL) AS addressexists
This works because TRUE is displayed as 1 in MySQL and FALSE as 0.
SELECT c.name, IF(a.addressid IS NULL,0,1) AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123
You do realize you are returning multiple data types in your query? This is not very good query design if the result is a query table.
The data types for 1 and 0 is integer (whether it is int, smallint, or tinyint) while data type for ‘’ is character.
You also need to define what ‘Blank’ is since this is not a proper value definition in MS SQL. I am going to assume that you want ‘Blank’ to be the same as ‘’ which is a zero-length string of type character (whether it is char, varchar, nchar, or nvarchar).
Another common problem is the use of ‘= NULL’ instead of the proper form ‘IS NULL’ since NULL itself is not a value; in simple terms NULL is the absence of a value.
You should read another post here by Larry Shanahan and his included linked post from Robert Sheldon:
Returning a NULL Value when query returns nothing
Post by Robert Sheldon re NULLs
Back to your SQL code - I believe a better simpler version would be one that avoids the use of NULL altogether and would be the following; this also keeps all returned types as character; very close to your 2nd query version:
CASE
WHEN PAE.SEX = 'F' THEN '1'
WHEN PAE.SEX = 'M' THEN '0'
ELSE ''
END AS SEX,
@larryshanahan
I need to return blank results when there is not a value entered into the table but have only been able to get either a null or 0 value.
CASE
WHEN PAE.SEX = ‘F’ THEN 1
WHEN PAE.SEX = ‘M’ THEN 0
WHEN PAE.SEX = NULL THEN ‘’
END AS SEX,
this returns a null value
CASE
WHEN PAE.SEX = ‘F’ THEN 1
WHEN PAE.SEX = ‘M’ THEN 0
ELSE ‘’
END AS SEX,
this returns a 0 value
I think you are making it harder than it should be.
If @UserRole is 'Analyst' then also SupervisorApprovedBy should be null? Else return everything?
WHERE (@UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL )
OR (ISNULL(@UserRole, '') <> 'Analyst')
The problem is likely the comparison to NULL, as explained in David Spillett's answer above. When @UserRole = 'Analyst', the comparison SupervisorApprovedBy = NULL will give UNKNOWN (and the row won't pass the WHERE test).
You can rewrite with nested CASE expressions:
WHERE 1 =
CASE
WHEN @UserRole = 'Analyst' THEN
CASE WHEN SupervisorApprovedBy IS NULL THEN 1 END
WHEN SupervisorApprovedBy IS NOT NULL THEN 1
END
Or a complicated CASE expression:
WHERE 1 =
CASE
WHEN @UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL THEN 1
WHEN @UserRole = 'Analyst' THEN 0
WHEN SupervisorApprovedBy IS NOT NULL THEN 1
END
or with a bit more easy to understand AND / OR:
WHERE
@UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL
OR @UserRole <> 'Analyst' AND SupervisorApprovedBy IS NOT NULL
OR @UserRole IS NULL AND SupervisorApprovedBy IS NOT NULL
Another issue is that SupervisorApprovedBy = SupervisorApprovedBy (and the equivalent SupervisorApprovedBy IS NOT NULL I used above) will not give you "all data". The rows where SupervisorApprovedBy is null will not be returned. If you do want them all, the conditions should be all adjusted:
WHERE 1 =
CASE
WHEN @UserRole = 'Analyst' THEN
CASE WHEN SupervisorApprovedBy IS NULL THEN 1 END
ELSE 1
END
WHERE 1 =
CASE
WHEN @UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL THEN 1
WHEN @UserRole = 'Analyst' THEN 0
ELSE 1
END
WHERE
@UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL
OR @UserRole <> 'Analyst'
OR @UserRole IS NULL
As others have mentioned you forgot to tell your CASE statement to return the number in case the number is not null.
However in SQL Server you can use NULLIF, which I consider more readable:
select
id,
firstname,
lastname,
nullif(number, 0) as number
from tperson;
If you want to stick to standard SQL then stay with CASE:
case when number = 0 then null else number end as number
SELECT ID, Firstname, Lastname,
CASE WHEN Number!=0 THEN Number END
FROM tPerson