I have a stored procedure which takes a #flag as a parameter. That flag supposes to indicate to select null values or none null values.
for none null values my solution looks like that:
#Flag int
SET #Flag = NULL
WHERE ISNULL(column1,'') = ISNULL(#Flag,'')
Is there a way to accommodate none null values in similar manner ? If no what would be the most compact solution ?
Flag int
SET #Flag = NULL
select * from table
WHERE (#flag is null and column is null) or ((#flag is not null and column is not null) and #flag = column)
I advise to NEVER SET ANSI_NULL OFF, NEVER! That can lead to a lot of unnecessary maintenance pain.
No need for a compact solution like
ISNULL(column1,'') = ISNULL(#Flag,'')
Also that ill return null rows when #flag = '' and also ill return '' rows when flag is null
The only way I can think of is SET ANSI_NULL OFF and then do comparison: column1 = #flag
Related
In my Postgres database, I'm checking user answers for correctness by checking if two IDs, "user_answered_id" and "expected_answer_id", are equivalent. If the user doesn't provide a "user_answered_id", then we still mark their answer as incorrect.
In Postgres, the following queries
select case when 1 != null then TRUE else FALSE end as test;
select case when 1 = null then TRUE else FALSE end as test;
both result in FALSE. This is true for any number check (e.g., when 2 != null, when 3 != null, ..., etc.
Why doesn't CASE WHEN show TRUE for 1 != null?
Must I put in the check "or is null"? E.g.,
CASE WHEN
user_answered_id != expected_answer_id
OR user_answered_id IS NULL
THEN TRUE
ELSE FALSE
END as user_incorrect_tally
What you are looking for is: IS DISTINCT FROM
select 2 is distinct from null;
?column?
----------
t
select 2 is distinct from 1;
?column?
----------
t
From the docs:
datatype IS DISTINCT FROM datatype → boolean
Not equal, treating null as a comparable value.
1 IS DISTINCT FROM NULL → t (rather than NULL)
NULL IS DISTINCT FROM NULL → f (rather than NULL)
SQL uses three-valued logic: true, false, and null. Null is not false. Null can be thought of as "no value".
Operations on null almost always yield null. So 1 != null is null. 1 = null is null. null = null is null. 5 < null is null. Etc.
To check for null, use is null and is not null.
Back to your query. is not distinct from and is distinct from are like = and != which treat null as a comparable value. So null is distinct from 1 will be true.
select
user_answered_id is distinct from expected_answer_id as user_incorrect
If you need to convert a null into a different value such as 0 or an empty string, use coalesce.
select
coalesce(user_answered_text, 'No Answer')
Your column is named "tally", but a tally means a count. If you intend to count a user's true and false answers use count with a filter.
select
count(user_answered_id) filter (
where user_answered_id = expected_answer_id
) as user_correct_tally,
-- count ignores null, this will only be the questions they tried to answer
count(user_answered_id) as user_answered_tally,
count(user_answered_id) filter (
where user_answered_is is distinct from expected_answer_id
) as user_incorrect_tally
Yes, You should check NULL value with is null, And last query you wrote is correct.
I suggest you to read below documents:
https://www.postgresql.org/docs/current/functions-comparison.html
Precise question.
Table ROW
value1 a
value2 b
value3 null
value4 d
Function parameters
CREATE OR REPLACE FUNCTION "GetValues"(
"#value1" VARCHAR(50),
"#value2" VARCHAR(50),
"#value3" VARCHAR(50),
"#value4" VARCHAR(50)
)
BEGIN
RETURN QUERY SELECT(
t."value1",
t."value2",
t."value3",
t."value4",
)
FROM "table" as t
WHERE t."value1" = COALESCE("#value1", c."value1")
AND t."value2" = COALESCE("#value2", c."value2")
AND t."value3" = COALESCE("#value3", c."value3")
AND t."value4" = COALESCE("#value4", c."value4");
END;
If I use the above function and only provide the following:
('a', null, null, 'd')
It will return [] even if 'a' and 'd' are found and I found that this only happens if I provide a parameter to search for something that is null and the value of the row is also null.
OLD DESCRIPTION BELOW
I have setup a get which uses COALESCE successfully to search by multiple or 1 parameter(s). However, if any one of those params that are not provided (so default to NULL) are actually NULL in the db because I haven't updated that field before, then it will always return an empty array, even though one of the provided params will successful match to a row in the table.
I just want to know if I need a new system all together to complete this search or if it is just an unfortunate effect of COALESCE?
Below is the relevant snippet.
FROM "table" as t
WHERE t."value1" = COALESCE("#value1", c."value1")
AND t."value2" = COALESCE("#value2", c."value2")
AND t."value3" = COALESCE("#value3", c."value3")
AND t."value4" = COALESCE("#value4", c."value4");
In the above, if I provide value1 and it matches but value4 is NULL in that row, then it will return [].
The return is a table with each of those 4 values.
Should this be a simple row comparison (give out all rows which have the same values as the input parameters)?
This could simply be achieved by the row comparator (documentation):
WHERE row(t.*) IS NOT DISTINCT FROM row("#value1", "#value2", "#value3", "#value4")
demo: db<>fiddle
If NULL as function input parameter should be a wildcard then #kurkle's solution works well.
You could do it like this:
FROM test as t
WHERE ("#value1" IS NULL OR t."value1" = "#value1")
AND ("#value2" IS NULL OR t."value2" = "#value2")
AND ("#value3" IS NULL OR t."value3" = "#value3")
AND ("#value4" IS NULL OR t."value4" = "#value4");
db<>fiddle
In Crystal Reports, is it possible to have a function that returns a numeric value if the if statement evaluates to true and returns NULL otherwise?
I currently have
IF ({INDICATOR} = 'Y') Then
(
{numeric value}
)
Else
(
0
);
But since 0 is a possible value of {numeric value} it doesn't make sense. Rather, I would rather have that field come up blank if the indicator isn't 'Y', but when I replace the 0 with NULL it gives me a type mismatch error.
Is there a way for me to only show the value when the indicator is 'Y'?
If you truly want a null value and not empty try the following
create a formula called NULL then save it and close without entering any data in the formula area. Then in your formula above try
If {INDICATOR} = 'Y' then {numeric value}
else tonumber({#NULL})
you can't return two different datatypes in a single if statement..If if is number then else should also be number.. instead try to split the statements and try.. something like below.
IF ({INDICATOR} = 'Y') Then
(
ToText({numeric value})
)
Else if ({INDICATOR} <> 'Y') Then
(
""
);
If {INDICATOR} = 'Y' then {numeric value}
else {Command.NULLCOL}
The setup in Database Expert is to use Add Command with sql:
select null as nullcol
from dual
Then left join to it.
A returned null value can be very powerful, so your need for a null value should not be questioned. Null values automatically display differently to stand out. Compared to 0 or "", null values work correctly with DistinctCount function. Null values also work correctly with section summaries and crosstabs, which can save you a lot of work which is the whole point of using crystal.
Which statement is perfect or better when dealing with billion of records for comparing NULL's in merge statement. I have tried with SET ANSI_NULLS OFF but that didn't work in merge statement. Here is my two ways
ISNULL(SRCColumn,-11111) = ISNULL(DSTColumn, -11111)
Or
SRCColumn = DSTColumn OR (SRCColumn IS NULL AND DSTColumn IS NULL)
Please let me know if there is any better way to deal with it. As I have around 15 columns to compare.
SRCColumn = DSTColumn OR (SRCColumn IS NULL AND DSTColumn IS NULL)
I'd suggest that you use this version because it most accurately expresses what you want SQL Server to do.
Both statements are logically equivalent (unless -11111 is a legal value for the column), however this statement is much more recognizable, and there's probably only a negligible difference in the run-time performance of the two statements.
If you are more concerned with succinctness than performance, CHECKSUM() is also an option. It will match NULL -> NULL:
MERGE A
USING B
ON A.Key = B.Key
WHEN MATCHED AND CHECKSUM(A.Col1, A.Col2, ... ) <> CHECKSUM(B.Col1, B.Col2, ... )
THEN UPDATE SET Col1 = B.Col1, Col1 = B.Col2, ...
How about using the NOT comparison on matching:
MERGE [TGT]
USING [SRC]
ON [SRC].Key = [TGT]. Key
…
WHEN MATCHED AND
(
NOT ([TGT].[dw_patient_key] = [SRC].[dw_patient_key] OR ([TGT].[dw_patient_key] IS NULL AND [SRC].[dw_patient_key] IS NULL))
OR NOT ([TGT].[dw_patient_key] = [SRC].[dw_patient_key] OR ([TGT].[dw_patient_key] IS NULL AND [SRC].[dw_patient_key] IS NULL))
...
)
THEN UPDATE
...
SSRS parameters are a pain. I want to be able to re-use reports for many different needs by allowing the users access to many different parameters and making them optional.
So, if I start out with code such as:
Select * from mytable myt
where myt.date between '1/1/2010' and '12/31/2010'
and year(myt.date) = '2010'
and myt.partnumber = 'XYZ-123'
I want those parameters to be optional so my first attempts were to make the parameters default to null such as:
and (myt.partnumber = (#PartNumber) or (#PartNumber) is null)
That has problems because if the database fields in question are nullable then you will drop records because null does not equal null.
I then used code such as this:
DECLARE #BeginDate AS DATETIME
DECLARE #EndDate AS DATETIME
DECLARE #PartNumber AS VARCHAR(25)
SET #Year = '..All'
SET #BeginDate = '1/1/2005'
SET #EndDate = '12/31/2010'
SET #PartNumber = '..All'
SET #Year = '..All'
Select * from mytable myt
where (myt.date between (#BeginDate) and (#EndDate))
and (year(myt.date) = (#Year) or (#Year) = '..All' )
and (myt.partnumber = (#PartNumber) or (#PartNumber) = '..All')
That doesn't work because Year(myt.date) is an integer and #Year is not.
So, here are my questions.
How can I make my dates optional? Is the best way to simply default them to dates outside of a practical range so I return all values?
What is the best way to handle the null or '..All' options to make my queries as readable as possible and allow my users to have optional parameters for most data types? I'd rather not use null for
Go ahead and allow nulls, which indicates the filter should not be applied. Then, you can use the following:
SELECT *
FROM mytable myt
WHERE COALESCE(myt.date, '1/1/1900') between COALESCE(#BeginDate, myt.date, '1/1/1900') and COALESCE(#EndDate, myt.date, '1/1/1900')
AND COALESCE(YEAR(myt.date), -1) = COALESCE(#Year, YEAR(myt.date), -1)
AND COALESCE(myt.partnumber, -1) = COALESCE(#PartNumber, myt.partnumber, -1)
In summary, if any variable value is NULL, then compare the column value to itself, which effectively ignores the condition. More specifically, when testing myt.date, if #BeginDate is NULL then set the lower range value equal to the myt.date value. Do the same substitution with the #EndDate value. Even, if both #BeginDate and #EndDate are NULL, the condition will be true.
A similar approach is used for YEAR(myt.date) and myt.partnumber. If the variable value is NULL, then compare the column value to itself, which is always true.
UPDATE:
Added a default value to each COALESCE to handle the situation where the column value is NULL.
I like your third code block. It seems like your WHERE clause could be corrected to work with a non-int value. The AND clause for the year line would look like this--not my best T-SQL, but it should get you pointed in the right direction:
and 1 = CASE #Year WHEN '..All' THEN 1 ELSE CASE WHEN year ( myt.date ) = CONVERT ( int, #Year ) THEN 1 ELSE 0 END END
This will allow you to have a string value of '..All' or an int value. Either will match correctly. You can do the same with partnumber.
try it like this, the key is to fix your null parameters values to surrogate nulls, also since sql server supports short circuit evaluation, putting the null check should generally perform better.
Select * from mytable myt
where (myt.date between (#BeginDate) and (#EndDate))
and (#Year IS NULL OR COALESCE(myt.date,'1900') = #Year)
and (#PartNumber IS NULL OR ISNULL(myt.partnumber, '<NULL>') = (#PartNumber)