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 OverflowVideos
You 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/
Use COALESCE:
SELECT COALESCE(field_a, field_b)
COALESCE is an ANSI standard function that returns the first non-null value from the list of columns specified, processing the columns from left to right. So in the example, if field_a is null, field_b value will be displayed. However, this function will return NULL if there is no non-null value from the columns specified.
It's supported on MySQL (I've used it on 4.1), SQL Server (since v2000), Oracle 9i+...
and another way to skin that cat (flexible for not just null comparisons)...
select if(field_a is not null, field_a, field_b) from...
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
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