Coalesce in where clause with n - sql

I am trying to get records based on the given input values. Below is the sample script
DECLARE input1 = '001'
DECLARE input2 = '002'
SELECT * FROM table WHERE COLUMN1 = COALESCE(input, NULL) OR
COLUMN2 = COALESCE(input2, NULL)// return non-null records, Great
DECLARE input1 = NULL
DECLARE input2 = NULL
SELECT * FROM table WHERE COLUMN = COALESCE(input, NULL) // return no records, Problem here
I know, COLUMN = NULL do not yield any values. Is there a better way, so that NULL input values return null records. Thanks in advance.

A "better" option might be to skip COALESCE (or NVL) and switch to
where column = input
or (column is null and input is null)

Your query:
SELECT *
FROM table
WHERE COLUMN1 = COALESCE(input, NULL)
OR COLUMN2 = COALESCE(input2, NULL)
Will only return results when COLUMN1 = input OR column2 = input. It will not return results when both columns are NULL.
You will get exactly the same results if you remove the COALESCE expressions:
SELECT *
FROM table
WHERE COLUMN1 = input
OR COLUMN2 = input2
If you want to check when the input is NULL and the column value is also NULL then you need to use IS to compare the values to NULL and not = like this:
SELECT *
FROM table
WHERE COLUMN1 = input -- Compare non-NULL values
OR ( input IS NULL AND COLUMN1 IS NULL ) -- Compare NULL values
OR COLUMN2 = input2 -- Compare non-NULL values
OR ( input2 IS NULL AND COLUMN2 IS NULL ) -- Compare NULL values

If you need to return rows with COLUMN1 is NULL in case of INPUT is null:
SELECT *
FROM table
WHERE
decode(COLUMN1,input, 1,0) = 1
Decode: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/DECODE.html#GUID-39341D91-3442-4730-BD34-D3CF5D4701CE
In a DECODE function, Oracle considers two nulls to be equivalent
You do not need coalesce in this query from your question:
SELECT *
FROM table
WHERE COLUMN1 = COALESCE(input, NULL) OR
COLUMN2 = COALESCE(input2, NULL)
because coalesce(input,null) is equal to simple input: coalesce returns first non-null parameter, but your second parameter is null, so it returns
input is not NULL | input
-------------------------
input is NULL | null

Related

use cast in SQL case statement

SELECT
CASE
WHEN column1 = 43 THEN CAST('abcde' as varchar(30))
WHEN column1 = 44 THEN CAST('fghij' as varchar(30))
ELSE 'N/A'
END AS value
Is the above case and cast statement correct? I need to convert the int to a varchar. column1 is a value that comes from another table.
Question 2: I have two values coming from two different tables (table1 and table2) and they need to go to the final table data type varchar
value 1 = CONVERT(VARCHAR(10),CAST(c.table1 * 100 AS DECIMAL(5,2))) + ' %' AS value1
value 2 = CONVERT(VARCHAR(10),c.table2,101) AS value2
c.table1 is decimal coming from table1
c.table2 is int coming from table2 and both need to be varchar at the final table
how do I combine both statements so that it can show up in the final table, its not an OR its an AND.
result set:

How to select all items where LEN > 0

I'm new to Google BigQuery, and I'm finding that the SQL really quite different from normal SQL. I'm trying to select all items from a table that has data in a field named 'ticket_fields'. I son't want nulls. I tried the following:
WHERE COLUMN <> ''
WHERE LEN(COLUMN) > 0
WHERE NULLIF(LTRIM(RTRIM(COLUMN)), '') IS NOT NULL
None of that worked. How can I select all records where there are non-nulls from one specific field?
In some systems, the empty string and NULL are treated equivalently (Oracle, for example). In BigQuery, these are distinct values, so:
-- Returns TRUE
SELECT CAST(NULL AS STRING) IS NULL
-- Returns NULL
SELECT LENGTH(CAST(NULL AS STRING)) > 0
-- Returns TRUE
SELECT CAST(NULL AS STRING) IS NULL
-- Returns FALSE
SELECT '' IS NULL
-- Returns FALSE
SELECT LENGTH('') > 0
-- Returns TRUE
SELECT '' IS NOT NULL
If you want to filter rows where the column is NULL, then use IS NOT NULL:
SELECT * FROM dataset.table WHERE column IS NOT NULL
If you want to filter rows where the column is empty or NULL, you can just check that the length is positive:
SELECT * FROM dataset.table WHERE LENGTH(column) > 0
This is because LENGTH(column) returns NULL if column is null, so the WHERE clause excludes the row.

Convert INT column values to an empty string using ISNULL

I need to convert column ID of INT data type to a empty string ['']. I should not modify the source column data type, but need to convert it in my transformation in the other table. The ID column is "nullable" as it has null in it.This is my code.
CREATE TABLE #V(ID INT) ;
INSERT INTO #V
VALUES (1),(2),(3),(4),(5),(NULL),(NULL) ;
SELECT CASE WHEN CAST(ISNULL(ID,'') AS VARCHAR(10)) = '' THEN '' ELSE ID END AS ID_Column
FROM #V;
this returns:
ID_Column
1
2
3
4
5
NULL
NULL
when I modify my CASE statement it as follows:
CASE WHEN CAST(ISNULL(ID,'') AS VARCHAR(10)) = '' THEN '' ELSE ID END AS ID_Column
it returns:
ID_Column
1
2
3
4
5
0
0
Is this what you want?
select coalesce(cast(id as varchar(255)), '')
from #v;
You have to turn the entire result column into a single column. If you want a blank value, then the type is some sort of character string.
In your examples, the else id means that the result from the case is an integer, which is why you are getting either 0 or NULL.

Use Ternary function in SQL Server to determine NULL | NOT NULL based on bit?

Say I have an input parameter to a stored procedure #flag
I want to filter my query based on some column col1 being null, based on the value of this flag.
Say if #flag = 1, show only records with col1 IS NULL, if #flag = 0, show only records with col1 IS NOT NULL
My intuition would lead me to this:
select *
from table1
where col1 IS IIF(#flag = 1, NULL, NOT NULL);
This does compile. Is there any other concise way to do this?
Even when your query compiles I doubt it will work. But you can do it with boolean and/or logic
select * from table1
where (#flag = 1 and col1 IS NULL)
or (#flag = 0 and col1 IS NOT NULL)

Using Case Statement in SQL with parameter/variable to check for Null values

I am trying to write a SQL Select statement to return records based on a user input through a front end.
I want to write the Select statement like this:
SELECT somefields
FROM sometable
WHERE CASE variable
WHEN 'blank' THEN field IS NULL
ELSE field = field
END
Basically I either want to filter a column to find NULL values or ignore the filter and return all values depending on the value of the variable. I know that the results of the CASE statement is not executable but how can I do this?
When variable is 'blank', the following query will give you rows where field is NULL. When variable is anything else, it will give you all rows:
SELECT somefields
FROM sometable
WHERE
(variable = 'blank' AND field IS NULL)
OR (variable <> 'blank')
You can use NULLIF() (link is for SQL Server, but NULLIF() should be standard):
SELECT somefields
FROM sometable
WHERE field = NULLIF(variable, 'blank')
The following snippet should behave as follows:
when #variable is null, return all rows
when #variable = 'blank', return all rows where field is null or field = 'blank'
otherwise, return rows where #variable equals field
Code snippet:
WHERE 1 = CASE
WHEN #variable is null then 1
WHEN #variable = 'blank' and field is null then 1
WHEN #variable = field then 1
END
SELECT somefields
FROM sometable
WHERE ((variable IS NULL OR variable = 0) OR (variable = field))
WHERE Criteria is apply when variable have value
For Example:
DECLARE #CityName VARCHAR(50)
SET #CityName = NULL
SELECT CityName
FROM City
WHERE ((#CityName IS NULL ) OR (#CityName = CityName ))
When City is null then tables return all rows
I think I get what you're after. Something like this maybe?
SELECT field1,
field2,
CASE variable
WHEN 'blank' THEN NULL
ELSE field3
END as field3
FROM sometable
Think I understand what you mean....for example....
SELECT
House, Postcode
from
SomeTable
where
(House=isnull(#House,House) or (House is null and #House is null))
and
(Postcode=isnull(#Postcode,Postcode) or (Postcode is null and #Postcode is null))
First bit of the conditional where is to use the variable, when present (the isnull bit is to ignore the variable if it's null)
Second bit of the conditional where is in case your evaluative field is null also as effectively fields don't = null they are 'is null'.
Confused? Good. Works on what I'm doing though!
Here is my solution based on #Andomar answer above aimed at anyone testing an input varchar value, you need to test the parameter in the right order as per the example below:
FIELD1 = CASE
WHEN #inputParameter = '' THEN FIELD1
WHEN #inputParameter <> FIELD1 THEN NULL -- if input is some text but does not match
WHEN #inputParameter IS NULL THEN FIELD1
WHEN #inputParameter != '' AND FIELD1 = #inputParameter THEN FIELD1
END
Hope this helps someone.