What is purpose of COALESCE = 0 - sql

There is some script that I seen where the code is as follows:
AND (PC.SystemID = #SystemID OR COALESCE(#SystemID, 0) = 0)
I understand COALESCE chooses the first not null but not sure what the purpose would be for = 0.

the where clause will return true for the whole AND
When
PC.SystemID = #SystemID
OR #SystemID is NULL
OR #SystemID = 0
so
#SystemID IS NULL OR #SystemID = 0 is equivalent to COALESCE(#SystemID, 0) = 0

Related

Not able to search the data in stored procedure

I need to search by Suburb however it does not return anything even though there's a data. Here is the stored procedure implementation and I have added it in the where statement. Please help me what cause the issue. Thank you.
WHERE
((#DriverId = '00000000-0000-0000-0000-000000000000') OR
(#DriverId IS NULL AND tl.T_DriverId IS NULL) OR
(#DriverId IS NOT NULL AND tl.T_DriverId = #DriverId)) AND
((#SearchBySuburb IS NOT NULL AND #Suburb = 0 AND pa.A_Suburb = #SearchBySuburb) AND
(#SearchBySuburb IS NOT NULL AND #Suburb = 1 AND da.A_Suburb = #SearchBySuburb)) AND
((#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status != 8) OR
(#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status IS NULL) OR
(#HideCompleted = 1 AND #StatusId > 0 AND tl.T_Status = #StatusId) OR
(#HideCompleted = 0 AND #StatusId = 0) OR
(#HideCompleted = 0 AND #StatusId > 0 AND tl.T_Status = #StatusId)) AND
(#OrganizationId IS NULL OR o.O_ID = #OrganizationId) AND
((#StartDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL) OR (#EndDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL) OR (tl.T_PlannedDeliveryDate BETWEEN CAST(#StartDate AS DATE) AND CAST(#EndDate AS DATE)))
The only time you use #SearchBySuburb is in the following fragment
((#SearchBySuburb IS NOT NULL AND #Suburb = 0 AND pa.A_Suburb = #SearchBySuburb) AND
(#SearchBySuburb IS NOT NULL AND #Suburb = 1 AND da.A_Suburb = #SearchBySuburb))
Given they're all AND, that part cannot ever evaluate as true because you're checking if #Suburb = 0 AND #Suburb = 1.
I suggest being very explicit with brackets and checking your ANDs and ORs.
Update following OP's comment about it still not working
I've re-formatted your WHERE into conceptual groups separated by AND clauses- see below. The groups are broadly about the driver, suburb, status, organisation, and start/end dates.
My suggestion is to
a) Start with none of them (which should therefore show all rows)
b) Add the groups (separated by ANDs) to the WHERE clause one at a time until your desired rows disappear
When your desired rows disappear, it tells you there's a problem with the last added filters to the WHERE clause.
WHERE
(
(#DriverId = '00000000-0000-0000-0000-000000000000')
OR (#DriverId IS NULL AND tl.T_DriverId IS NULL)
OR (#DriverId IS NOT NULL AND tl.T_DriverId = #DriverId)
)
AND
(
(#SearchBySuburb IS NOT NULL AND #Suburb = 0 AND pa.A_Suburb = #SearchBySuburb)
OR (#SearchBySuburb IS NOT NULL AND #Suburb = 1 AND da.A_Suburb = #SearchBySuburb)
)
AND
(
(#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status != 8)
OR (#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status IS NULL)
OR (#HideCompleted = 1 AND #StatusId > 0 AND tl.T_Status = #StatusId)
OR (#HideCompleted = 0 AND #StatusId = 0)
OR (#HideCompleted = 0 AND #StatusId > 0 AND tl.T_Status = #StatusId)
)
AND
(#OrganizationId IS NULL OR o.O_ID = #OrganizationId)
AND
(
(#StartDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL)
OR (#EndDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL)
OR (tl.T_PlannedDeliveryDate BETWEEN CAST(#StartDate AS DATE) AND CAST(#EndDate AS DATE))
)

SQL Server - CASE on where clause

I'm working on a stored procedure that is a select query. Under the WHERE clause I am filtering the return results to return only if there is a PageTitle= 'STA'
Here is am example of my query :
#InputCountryId INT,
#InputIndustryId INT
AS
BEGIN
SELECT
r.IdCountry,
r.IdIndustry,
r.PageTitle,
r.IndustryName
From dbo.Reports r
WHERE
r.IdCountry = #InputCountryId
r.IdIndustry = #InputIndustryId
r.PageTitle = 'STA'
END
The ending condition r.PageTitle I would like it to be applied ONLY IF InputCountry = 1 if not; do not include the filter.
I've attempted this by including a CASE. I am having a syntax error any time I try and introduce this case. Is this something that is possible? Am I implementing it incorrectly?
Here is an example of the stored proc with the CASE included.
#InputCountryId INT,
#InputIndustryId INT
AS
BEGIN
SELECT
r.IdCountry,
r.IdIndustry,
r.PageTitle,
r.IndustryName
From dbo.Reports r
WHERE
r.IdCountry = #InputCountryId
r.IdIndustry = #InputIndustryId
CASE WHEN #InputCountryId = 1 THEN
r.PageTitle = 'STA'
END
END
Try it this way:
WHERE
r.IdCountry = #InputCountryId and
r.IdIndustry = #InputIndustryId and
(#InputCountryId <> 1 or r.PageTitle = 'STA')
You dont need case statement. You can use OR clause
WHERE
r.IdCountry = #InputCountryId
r.IdIndustry = #InputIndustryId
(#InputCountryId != 1 OR r.PageTitle = 'STA')
this only filters PageTitle with STA when InputCountry is 1
I can't test with your data, but here is a CASE in a WHERE clause that works.
DECLARE #Variable INT = 3
SELECT GETDATE()
WHERE 1 =
CASE
WHEN #Variable = 1 THEN 1
WHEN #Variable = 3 THEN 1
WHEN #Variable = 5 THEN 1
ELSE 0
END

IF Else statements in SQL Update Query

I want to write if else in SQL Server Update Query.
UPDATE [dbo].[TempTable]
SET [Level1] = CASE WHEN A = -1 THEN 0 ELSE A END
,[Level2] = CASE WHEN A = -1 THEN 0 ELSE A END;
You need to use CASE in such scenarios. Try the above query..
If you insist on using IF, you can try the following:
IF
IF A = -1 BEGIN
UPDATE [dbo].[TempTable]
SET [Level1] = 0
,[Level2] = 0
END
ELSE BEGIN
UPDATE [dbo].[TempTable]
SET [Level1] = A
,[Level2] = A
END
IIF
UPDATE [dbo].[TempTable]
SET [Level1] = IIF(A = -1, 0, A)
,[Level2] = IIF(A = -1, 0, A)
You can use CASE statement, and If "A" is one of the column in your dbo.TempTable
DECLARE #Table TABLE(A INT,Level1 INT,Level2 INT)
INSERT #Table
SELECT -1, NULL, NULL UNION ALL
SELECT 1,NULL,NULL
UPDATE t SET
Level1 = CASE WHEN A = -1 THEN 0 ELSE A END,
Level2 = CASE WHEN A = -1 THEN 0 ELSE A END
FROM #Table t
SELECT * FROM #Table
-- Result
A Level1 Level2
-1 0 0
1 1 1
With case or IIF ;)
UPDATE [dbo].[TempTable]
SET [Level1] = IIF(A = -1 , 0 , A), [Level2] = IIF(A = -1 , 0 , A)
This will work:
UPDATE [dbo].[TempTable]
SET [Level1] = IIF(A = -1, 0, A), [Level2] = IIF(A = -1, 0, A);

SQL working strange

When I execute this SQL code:
select id, opis, vidrabota, tipprov, hitnost, valuta, drzava, zbirnaprov, tip_zbirprov, kanal
from dev_1450autoebanktip
where opis is null or Opis like case when isnull('','') = '' then Opis else '%' + '' + '%' end
and VidRabota = case when isnull('','') = '' then VidRabota else '' end
and TipProv = case when isnull(0,0) = 0 then TipProv else 0 end
and Valuta = case when isnull('','') = '' then Valuta else '' end
and drzava is null or Drzava = case when isnull('','') = '' then Drzava else '' end
I get this set of results:
But when I add one more condition (last row):
select id, opis, vidrabota, tipprov, hitnost, valuta, drzava, zbirnaprov, tip_zbirprov, kanal
from dev_1450autoebanktip
where opis is null or Opis like case when isnull('','') = '' then Opis else '%' + '' + '%' end
and VidRabota = case when isnull('','') = '' then VidRabota else '' end
and TipProv = case when isnull(0,0) = 0 then TipProv else 0 end
and Valuta = case when isnull('','') = '' then Valuta else '' end
and drzava is null or Drzava = case when isnull('','') = '' then Drzava else '' end
and KANAL = case when isnull(0,0) = 0 then KANAL else 0 end
I am losing one row in the result. What is causing this change?
Kanal is NULL in the last row.
and KANAL = case when isnull(0,0) = 0 then KANAL else 0 end
boils down to
and KANAL = KANAL
But this is not true for NULL, because NULL is the unknown value. When comparing null with null the result is neither true nor false, but unknown. Thus the added criteria dismisses the last record.
There is one thing I'd like to add: Use parentheses when mixing AND and OR. For instance
a = b or a = c and d = e
means
a = b or (a = c and d = e)
because AND has precedence over OR and you may want the expression to mean
(a = b or a = c) and d = e
Use parentheses in order to avoid any mistakes.

comparing three variables

I have three integer variables: firstCount, secondCount, thirdCount. I need to compare it and to output the result. I'm bad at SQL, but C# code is something like this:
if(firstCount == secondCount == thirdCount)
return true;
else
return false;
One way (2008 syntax).
SELECT CAST(CASE
WHEN COUNT(DISTINCT C) = 1 THEN 1
ELSE 0
END AS BIT)
FROM (VALUES (#firstCount),
(#secondCount),
(#thirdCount)) t (C)
declare #first int
, #second int
, #third int
select #first = 0
, #second = 0
, #third = 0
select case when (#first = #second) AND (#second = #third) THEN 1 ELSE 0 END
Following is one way to do it but after reading #meagar's comment, I find that solution to be more elegant. Just waiting for him to turn it into an answer...
DECLARE #firstcount INTEGER
DECLARE #secondcount INTEGER
DECLARE #thirdcount INTEGER
SET #firstcount = 1
SET #secondcount = 2
SET #thirdcount = 3
IF #firstcount <> #secondcount SELECT 0
ELSE IF #secondcount <> #thirdcount SELECT 0
ELSE SELECT 1
You are wanting to say If Firstcount is equal to second count, and secondcount is equal to third count return true.
You can just do
DECLARE #a INT = 2
DECLARE #b INT = 2
DECLARE #c INT = 2
IF (#a = #b AND #a = #c)
BEGIN
Print('true')
END
ELSE
BEGIN
Print('false')
END
Although it makes no difference you dont need to test B = C because A = C is exactly the same thing since all 3 values have to be the same.
Try the code ,
if ( (firstCount = secondCount) and (secondCount = thirdCount) and (firstCount = ThirdCount) )
return true;
else
return false;