IF Else statements in SQL Update Query - sql

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);

Related

What is purpose of COALESCE = 0

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

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))
)

adding two numeric values results null value in stored procedure

In a stored procedure I am trying to add two numeric values as shown
DECLARE #tradeamt1 NUMERIC(23,2)
DECLARE #tradeamt3 NUMERIC(23,2)
DECLARE #value NUMERIC(23,2)
if (#retVal1 = 0)
BEGIN
SELECT #row_count = count(1),
#tradeamt1=Sum(trade_amt) ,
#units= Sum(curr_shrs_num)
FROM [csr_staging].[dbo].[fi_impact_source] WITH(NOLOCK)
Where acct_id = 'BANKFEES'
and SD_ID >= EFF_DT
print #tradeamt1
END
if(#retVal3 > 0)
BEGIN
select #row_count = count(1) - #retVal3 + #row_count,#tradeamt3=Sum(trade_amt),#units= #units +Sum(curr_shrs_num) - #currshares
FROM [CSR_Staging].[dbo].[fi_impact_source] WITH(NOLOCK)
where (clearing_code = 'MBS'or clearing_code = 'CNS') and (SD_ID >= EFF_DT)
print #tradeamt3
END
set #value = #tradeamt1 +#tradeamt3
this value gives null instead of adding tradeamount1 =1.00 and tradeamount3 = 191432650.13
Maybe this could work for your SP:
declare #tradeamt1 numeric(23, 2) = 0
, #tradeamt3 numeric(23, 2) = 0
, #value numeric(23, 2) = 0
select #row_count = 0, #units = 0
if #retVal1 = 0
select #row_count = count(1)
, #tradeamt1 = isnull(sum(trade_amt), 0)
, #units = isnull(sum(curr_shrs_num), 0)
from [CSR_Staging].[dbo].[fi_impact_source] with (nolock)
where acct_id = 'BANKFEES'
and SD_ID >= EFF_DT
if #retVal3 > 0
select #row_count = #row_count + count(1) - #retVal3
, #tradeamt3 = isnull(sum(trade_amt), 0)
, #units = #units + isnull(sum(curr_shrs_num), 0) - #currshares
from [CSR_Staging].[dbo].[fi_impact_source] with (nolock)
where clearing_code in ('MBS', 'CNS')
and SD_ID >= EFF_DT
set #value = #tradeamt1 + #tradeamt3
Using isNull( ensures that both #tradeamt1 and #tradeamt3 are not null
This is most probably because when tradeamt1 and tradeamt3 are not initialised and for some reason also not assigned values.
You could initialise them with:
DECLARE #tradeamt1 NUMERIC(23,2) = 0
DECLARE #tradeamt3 NUMERIC(23,2) = 0
DECLARE #value NUMERIC(23,2) = 0
Let me know if that helps.
:)

SQL Server conditional where clause

I have the following where clause that begins with the following:
DECLARE #Birthdate = 1
SELECT ...
FROM ...
JOIN ...
WHERE A.organization_id = 1
AND LEFT(A.[first_name], 3) = LEFT(B.[first_name], 3)
AND LEFT(A.[last_name], 3) = LEFT(B.[last_name], 3)
After this I would like to conditionally execute additional clauses.
For example:
if #Birthdate = 1 then add AND A.[birthdate] = B.[birthdate]
otherwise, don't add anything
I've tried the following and none seem to work properly:
Ex 1:
AND (#Birthdate = 1 AND A.[birthdate] = B.[birthdate])
Ex 2:
AND ((#Birthdate = 1 AND A.[birthdate] = B.[birthdate]) OR
(#Birthdate = 0 AND (A.[birthdate] = B.[birthdate] OR
A.[birthdate] <> B.[birthdate])))
Any suggestions as to what I'm doing wrong?
You can use an or to apply your filter, or to just return all rows when #Birthdate = 0
SELECT
...
FROM
...
WHERE
(
-- Apply the filter if applicable
(
#Birthdate = 1
and A.[birthdate] = B.[birthdate]
)
-- Or return everything
or #Birthdate <> 1
)

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;