Can someone please explain to me in laymen terms why do we put ISNULL? I have an understanding of IS NULL but can't seem to put it together in this CASE context and what would be the impact if you didn't put it.
UpdateTime refers to when a particular row has been updated with new information
Example 1:
CASE WHEN ISNULL(DB1.UpdateTime,'') >= ISNULL(DB2.UpdateTime,'') THEN ISNULL(DB1.UpdateTime,'')
ELSE DB2.UpdateTime
END AS UpdateTime
FROM dbo.DB1_Result DB1 INNER JOIN dbo.DB2 DB2
ON DB1.ID = DB2.ID
Example 2:
StartTime = 01/01/2022
WHERE
(ISNULL(DB1.UpdateTime,'') >= #StartTime
Thank you!
SQL uses trinary logic: true, false, and null. null means the value is unknown, it could be anything.
If you try to compare anything with null you get null, even null. null = anything is null. null <> anything is null. null < anything is null. null > anything is also null.
Null is neither equal nor not equal to itself. null = null is null and null <> null is null. This is why we write x is null not x = null.
If you were to write
case
when DB1.UpdateTime >= DB2.UpdateTime then
db1.UpdateTime
else
db2.UpdateTime
end
and if either one is null the DB1.UpdateTime >= DB2.UpdateTime will be null. Case will treat that as false and return DB2.UpdateTime. If either value is null, you always get DB2.UpdateTime even if it's null and DB1.UpdateTime is not.
They don't want that. If DB2.UpdateTime is null, they want to return DB1.UpdateTime. If DB1.UpdateTime is null, they want to return DB2.UpdateTime. So they've used isnull to convert null to something that will compare as less than any time; '' works for that.
Demonstration.
Note: ELSE DB2.UpdateTime may want to be ELSE isnull(DB2.UpdateTime, '') to be consistent with THEN ISNULL(DB1.UpdateTime,'').
Note: isnull is a SQL Server extension. The SQL standard is coalesce.
Note: '' is not a timestamp. It happens to work, but other SQL servers will reject it. 1900-01-01 would be better.
Related
I am trying to return a result for 2 date columns based on different values like this:
CASE WHEN game_startA IS NOT NULL THEN game_startA
WHEN game_startB IS NOT NULL THEN game_startB
ELSE ''
END AS 'Game Start Date'
,
CASE WHEN game_endA IS NOT NULL THEN game_endA
WHEN game_endB IS NOT NULL THEN game_endB
WHEN COALESCE(game_endC, game_endD) IS NOT NULL THEN COALESCE(game_endC, game_endD)
ELSE ''
END AS 'Game End Date'
The problem is, if the value is NULL, it is returning 1900-01-01 instead of saying NULL or just blank.
Is there a way to fix that?
This logic could be vastly simplified (unless there is more to it than you've shared):
COALESCE(game_startA, game_startB) AS [Game Start Date],
COALESCE(game_endA, game_endB, game_endC, game_endD) AS [Game End Date]
I don't see any need for the CASE expressions or any ELSE that tries to turn a date into an empty string. If COALESCE() gets to the end of its list and still doesn't find a non-NULL value, the output is what you want: NULL.
How can I return IS NOT NULL on from a SQL Server CASE statement?
I have tried something like this:
WHERE
a.BillToCustomerID IS NOT NULL
AND CASE #taxSchedRequiered
WHEN 1 THEN a.TaxScheduleID IS NOT NULL
END
but it is not working for me.
Update
I need to filter by a.TaxScheduleID IS NOT NULL, just in the specific case when the value of the variable #taxSchedRequiered (possible values are: null, 0, 1) is 1.
just change the logic and use an OR
WHERE
a.BillToCustomerID IS NOT NULL
AND (#taxSchedRequiered <> 1 OR a.TaxScheduleID IS NOT NULL)
this will include all records if #taxSchedRequiered = 0 else it will only include records that have a a.TaxScheduleID <> NULL
Also make sure that #taxSchedRequired has a value other than NULL when you execute this
Edit to check for null param
WHERE
a.BillToCustomerID IS NOT NULL
AND (ISNULL(#taxSchedRequiered,0) = 0 OR a.TaxScheduleID IS NOT NULL)
Below is my where condition in query
WHERE ResetID= CASE WHEN NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item) THEN NULL
WHEN #LastIssued < #LastDispatched THEN 1
WHEN #LastIssued >= #LastDispatched THEN 2
END
If case statement returns null the where condition will not work.. Any alternate solution for this condition without case
If you want to have NULL = NULL, you need to handle it with IS NULL. At a guess, what you want is this:
WHERE (ResetID = CASE WHEN NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item) THEN NULL
WHEN #LastIssued < #LastDispatched THEN 1
WHEN #LastIssued >= #LastDispatched THEN 2
END
OR (ResetID IS NULL AND NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item)))
Edit note that this does require 2 scans of the table ops.table; there may be a better way to do this, but difficult to suggest when we only have a snippet.
I'm trying to run the following case statement in SQL:
CASE
WHEN P.Photos_Cloned_From IS NULL
THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
The statements works, but only for those records that are not NULL. For items that are NULL, the statement is not returning the THEN condition.
Any suggestions?
Try this and let us know what happens.
'http://www.toolboxbarn.com/v/vspfiles/photos/'+
COALESCE(P.Photos_Cloned_From,P.ProductCode,'DEFAULT')+'-2T.jpg'
AS image_link,
Using coalesce here is better than the case statement. Some platforms can optimize coalesce and it lets you easily make a default value.
#hogan's Suggestion is a great one to save code, but ultimately it should have the same result as your case statement if you don't introduce his new 'default' case. It is most likely that the ProductCode is also NULL is that a possibility?
Why string + NULL = NULL because NULL is an unknown in sql most db platforms will nullify the entire value when null is aggregated or concatenated.
So Think of the following test cases:
Id ProductCode Photos_Cloned_From
1 1 ClonedFrom
2 2 Null
3 NULL Null
The results of your case expression and Hogan's Suggestion would be:
1) ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/ClonedFrom-2T.jpg'
2) WHEN 'http://www.toolboxbarn.com/v/vspfiles/photos/2-2T.jpg'
3) WHEN NULL
CASE WHEN ISNULL(P.Photos_Cloned_From,'') = '' THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
SELECT
F.Name
, F.Address
, F.InDate
, ( CASE WHEN (F.Date_Down IS NULL and F.Cod_Down = 0 )THEN 0 ELSE -1 END) as 'sn_Down'
FROM
Folders F
I want to have a simple condition in my where clause, on one of the columns like:
When #PostTypeid is null then where should be PostTypeid in (4,5) and if
#PostTypeid is not null then where clause should be PostTypeId = #PostTypeId
I am trying to use case for this purpose but I think I am getting confused with the syntax. Here is what I have come up with so far:
(PostTypeid = case(#PostTypeId) when null then in (4,18) else #PostTypeId end)
The error is when I use in, I dont think that's a valid syntax. What is the correct way to do this? thanks. I Tried shifting the braces to several other places but in vain.
Thanks
Something like:
WHERE
(#PostTypeID IS NULL AND PostTypeId IN (4, 5)) OR
(#PostTypeId IS NOT NULL AND PostTypeID = #PostTypeID)
perhaps?
You cant use = and in operators in one statement
EDIT:
(PostTypeid = case(#PostTypeId) when null then in (4,18) else #PostTypeId end)
if the #PostTypeId is null then it will goes like this.
(PostTypeid = in (4,8))
and this is syntax error.
case(#PostTypeID) when IS NULL then PostyTypeId in (4,18) else PostTypeId = PostTypeId
this case should return single result.