I'm trying to find the even or odd numbers from given values, the thing is I want to update the E_and_o column which is resulted in select statement. can someone help me please.
Don't know where I'm doing it wrong.
Create table EvenRodd(
Nrow INT,
E_and_O VARCHAR(15));
///Select statement//
Select nrow, E_and_O,
Case
When Mod(nrow, 2) != 0 then 'Odd'
When Mod(nrow, 2) = 0 then 'Even'
end
From EvenRodd;
//Update //////
Update evenrodd set e_and_o = (Select evenrodd.nrow,
Case
When Mod(evenrodd.nrow, 2) != 0 then 'Odd'
When Mod(evenrodd.nrow, 2) = 0 then 'Even'
end
From EvenRodd) WHERE e_and_o IS NULL;
Update evenrodd set e_and_o =
Case
When Mod(nrow, 2) != 0 then 'Odd'
When Mod(nrow, 2) = 0 then 'Even'
end
EDIT: if you want to set e_and_o as computed column, and not update it every time:
ALTER TABLE evenrodd DROP COLUMN e_and_o;
ALTER TABLE evenrodd ADD e_and_o AS
Case
When Mod(nrow, 2) != 0 then 'Odd'
When Mod(nrow, 2) = 0 then 'Even'
end;
I would bet that you pass too many values out from inner select query. Try something like this:
/Update //////
Update evenrodd set e_and_o = (Select
Case
When Mod(evenrodd.nrow, 2) != 0 then 'Odd'
When Mod(evenrodd.nrow, 2) = 0 then 'Even'
end
From EvenRodd) WHERE e_and_o IS NULL;
Related
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
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);
Hi I am trying to execute following Update statement
UPDATE rebate_admin.PRODUCT_REBATE_ITEM_RULE_GROUP SET rule_priority_Nbr =
CASE
WHEN ( rule_priority_Nbr = 3) THEN (rule_priority_Nbr+1)
WHEN ( rule_priority_Nbr = 4 ) THEN (rule_priority_Nbr-1)
END
WHERE DEFAULT_RULE_IND = 1 AND ACTIVE_IND= 1 AND RULE_PRIORITY_NBR NOT IN (9999999900, 1)
AND PRODUCT_REBATE_DETAIL_SEQ IN (843 )
It fails with error:
Cannot update (%s) to NULL.
You have a NOT NULL constraint on rule_priority_Nbr column. Your CASE statement doesnt have the ELSE, so when rule_priority_Nbr not 3 and not 4 the CASE will return null
Try like this for instance
UPDATE rebate_admin.PRODUCT_REBATE_ITEM_RULE_GROUP
SET rule_priority_Nbr =
CASE WHEN ( rule_priority_Nbr = 3) THEN (rule_priority_Nbr+1)
WHEN ( rule_priority_Nbr = 4 ) THEN (rule_priority_Nbr-1)
ELSE rule_priority_Nbr
END
WHERE DEFAULT_RULE_IND = 1
AND ACTIVE_IND= 1
AND RULE_PRIORITY_NBR NOT IN (9999999900, 1)
AND PRODUCT_REBATE_DETAIL_SEQ IN (843 )
I have one CASE WHEN condition (CompanyGUID and LineGUID) and I need 4 different calculations with 4 different columns.
The calculation works fine, but I just wonder is any way to make it more convenient? like for example maybe using COALESCE trick in this case?
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN isnull(ddaWC.archexMod,1)
END as ExperienceMod,
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN case when convert(int,ddawc.premModTtl) <= 0 then 1
when convert(int,ddawc.premModTtl) >= 0
then (1 + ddaWC.premschmod / ddawc.premModTtl) end
END as ScheduleMod,
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN isnull(ddaWC.TMpercent,1) END as TerritoryMod,
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN case when convert(int,ddaWC.SchedPercent) = 0 or
ddaWC.SchedPercent is not null
then (isnull(ddaWC.archexMod,1)
* (convert(decimal(5,2),isnull(ddaWC.SchedPercent,1))))
* isnull(ddaWC.TMpercent,1)
when ddaWC.SchedPercent is null
then 1 END
END as EffectiveMod
Something like this? The OUTER APPLY will perform a row-based select which return NULLs in case of your condition not being fullfilled. Otherwise it will return the same values you've specified. This will only work, if you really need the same logic for all columns.
SELECT otherColumn
,ConditionalColumns.*
FROM YourTable
OUTER APPLY
(
SELECT isnull(ddaWC.archexMod,1) as ExperienceMod,
case when convert(int,ddawc.premModTtl) <= 0 then 1
when convert(int,ddawc.premModTtl) >= 0
then (1 + ddaWC.premschmod / ddawc.premModTtl) end as ScheduleMod,
isnull(ddaWC.TMpercent,1) as TerritoryMod,
case when convert(int,ddaWC.SchedPercent) = 0 or ddaWC.SchedPercent is not null
then (isnull(ddaWC.archexMod,1)
* (convert(decimal(5,2),isnull(ddaWC.SchedPercent,1))))
* isnull(ddaWC.TMpercent,1)
when ddaWC.SchedPercent is null
then 1 END as EffectiveMod
WHERE dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
) AS ConditionalColumns
Like this:
somewhere before code:
Declare #compGuid char(37) = '29634AF7-D0A2-473D-9574-405C23E10F02'
Declare #lineGuid char(37) = '1CB72920-B3FC-4822-8030-37B50A2810EB'
and modify From Clause in SQL statement to define aliases
cl for tblCompanyLocations, and
q for tblQuotes
then the case in the Select clause of the SQL code could be reduced to:
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then isnull(ddaWC.archexMod,1) end ExperienceMod,
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then case when cast(ddawc.premModTtl as int) > 0
then (1 + ddaWC.premschmod / ddawc.premModTtl)
else 1 end end ScheduleMod,
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then isnull(ddaWC.TMpercent,1) end TerritoryMod,
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then case when cast(ddaWC.SchedPercent as int) = 0
then isnull(ddaWC.archexMod,1)
* cast(ddaWC.SchedPercent as decimal(5,2)))
* isnull(ddaWC.TMpercent,1)
when ddaWC.SchedPercent is null
then 1 end end EffectiveMod
which is a bit more readable... (check the logic though)
I want the where statement for this query to be optional depending on what the value of parameter #RetrieveAll is. If #RetrieveAll is false/null the where statement is used, if it is true it should be ignored.
#IncludeErrors bit = 1,
#IncludeAccess bit = 1,
#IncludeLogins bit = 1,
#RetrieveAll bit = NULL
SELECT
//...
FROM
(
) AS a
WHERE
a.RowNumber BETWEEN #ItemCountStart AND #ItemCountEnd
Is there way to do this?
SELECT
//...
FROM
(
) AS a
WHERE
(a.RowNumber BETWEEN #ItemCountStart AND #ItemCountEnd AND #RetrieveAll IS NULL)
OR
#RetrieveAll IS NOT NULL
You should try this,
Select * from Tablename a
Where
1 = case when isnull(#RetriveAll,0) == 0 then
case when a.RowNumber BETWEEN #ItemCountStart AND #ItemCountEnd then 1 else 0 end
else 1 end