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 Overflow
🌐
W3Schools
w3schools.com › sql › sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
The following SQL lists all customers with a value in the "Address" field: SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL; Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
🌐
W3Schools
w3schools.com › sql › sql_isnull.asp
SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... Suppose that the "UnitsOnOrder" column is optional, and may contain NULL values. ... In the example above, if any of the "UnitsOnOrder" values are NULL, the result will be NULL.
🌐
W3Schools
w3schools.com › sql › func_mysql_ifnull.asp
MySQL IFNULL() Function
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The IFNULL() function returns a specified value if the expression is NULL.
🌐
StrataScratch
stratascratch.com › blog › introduction-to-ifnull-function-in-sql
Introduction to IFNULL() Function in SQL - StrataScratch
If NULL, the second expression is evaluated, and so on, until a NON-NULL value is found. In this example, we apply the SQL IFNULL() function to a hard-level interview question asked by Google.
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › t-sql › language-elements › nullif-transact-sql
NULLIF (Transact-SQL) - SQL Server | Microsoft Learn
USE AdventureWorks2022; GO SELECT ProductID, MakeFlag, FinishedGoodsFlag, NULLIF(MakeFlag,FinishedGoodsFlag) AS [Null if Equal] FROM Production.Product WHERE ProductID < 10; GO SELECT ProductID, MakeFlag, FinishedGoodsFlag, [Null if Equal] = CASE WHEN MakeFlag = FinishedGoodsFlag THEN NULL ELSE MakeFlag END FROM Production.Product WHERE ProductID < 10; GO
Find elsewhere
🌐
DataFlair
data-flair.training › blogs › sql-null-functions
SQL Null Functions - ISNULL, IFNULL, Combine, & NULLIF - DataFlair
March 11, 2021 - Null functions are required to perform operations on the null values stored in our database. We can perform functions on NULL values, which explicitly recognize if a value is null or not. Using this recognizing capacity, one can further perform operations on the null values like the aggregate functions in SQL.
🌐
TechOnTheNet
techonthenet.com › sql › is_null.php
SQL: IS NULL Condition
This SQL tutorial explains how to use the SQL IS NULL condition with syntax and examples. The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE.
🌐
W3Schools
w3schools.com › mysql › mysql_ifnull.asp
MySQL IFNULL() and COALESCE() Functions
The MySQL IFNULL() function lets you return an alternative value if an expression is NULL. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · If you want to ...
Top answer
1 of 4
8

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')
2 of 4
13

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       
🌐
TablePlus
tableplus.com › blog › 2019 › 09 › sql-if-null-then-0.html
SQL IF NULL THEN 0 | TablePlus
September 11, 2019 - In MySQL you can also use IFNULL function to return 0 as the alternative for the NULL values: SELECT emp_no, salary, from_date, to_date, IFNULL(bonus, 0) FROM salaries; In MS SQL Server, the equivalent is ISNULL function:
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › t-sql › queries › is-null-transact-sql
IS NULL (Transact-SQL) - SQL Server | Microsoft Learn
If the value of expression is NULL, IS NOT NULL returns FALSE; otherwise, it returns TRUE. To determine whether an expression is NULL, use IS NULL or IS NOT NULL instead of comparison operators (such as = or !=). Comparison operators return ...
🌐
ThoughtSpot
thoughtspot.com › sql-tutorial › sql-is-null
SQL IS NULL | Basic SQL | ThoughtSpot
3 weeks ago - This SQL tutorial covers how to work with null values by using SQL's IS NULL operator to select rows where a column contains no data.
🌐
w3resource
w3resource.com › mysql › control-flow-functions › if-null-function.php
MySQL IFNULL() function - w3resource
NULLIF(): This function returns NULL if the two arguments are equal; otherwise, it returns the first argument. It is used for a different purpose than IFNULL(). 8. Why is it important to handle NULL values in SQL?
🌐
DataCamp
datacamp.com › doc › mysql › mysql-ifnull
MySQL IFNULL Keyword: Usage & Examples
sql SELECT product_name, price, quantity, IFNULL(price * quantity, 0) AS total_value FROM products; This example uses `IFNULL` to ensure that any NULL `price` or `quantity` results in a `total_value` of `0`, preventing calculation errors.