I have a table TEST with columns VALUE,VALUE_SIM,SIM_STATUS,ID. I want to update the column SIM_STATUS for the ID = 288. I also want to display the columns after updating.
The conditions are :
1. SIM_STATUS = 0 when VALUE = VALUE_SIM.
2. SIM_STATUS = 1 when VALUE < VALUE_SIM.
3. SIM_STATUS = 2 when VALUE > VALUE_SIM.
I wrote the following query but it is showing an error.
("UPDATE TEST"
"SET SIM_STATE = ( CASE WHEN VALUE = VALUE_SIM THEN SIM_STATE = 0 END )"
"SET SIM_STATE = ( CASE WHEN VALUE < VALUE_SIM THEN SIM_STATE = 1 END )"
"SET SIM_STATE = ( CASE WHEN VALUE > VALUE_SIM THEN SIM_STATE = 2 END )"
"where ID = 288 ");
The query that you want is:
UPDATE TEST
SET SIM_STATE = (CASE WHEN VALUE = VALUE_SIM THEN 0
WHEN VALUE < VALUE_SIM THEN 1
WHEN VALUE > VALUE_SIM = 2
END)
WHERE NUMBER = 288;
Your query has several syntax errors. I don't even know if you intend for the double quotes to be part of the query.
This would do i guess
UPDATE TEST
SET
SIM_STATE =
CASE WHEN VALUE < VALUE_SIM THEN 1
+ CASE WHEN VALUE > VALUE_SIM THEN 2
+ CASE WHEN VALUE = VALUE_SIM THEN 0
WHERE ID = 1
Related
I have a view which "web.individual_usage_vw".
Whose total count is = 39057.
SQL Query: select count(*) from web.individual_usage_vw;
In this view, it has few columns in which it has numeric data.
So I need to fetch all the records from view where data is > 1.
So I used below query:
select count(*) from web.individual_usage_vw
where "Business & economy" != 0
and "Executive rewards" != 0
and "Health & benefits" != 0
and "Investment" != 0
and "Corporate marketing" != 0
and "Retirement" != 0
and "In General" != 0
and "Mergers & acquisitions" != 0
and "Corporate strategy operations" != 0
and "Broad-based rewards" != 0
and "Leadership" != 0
and "Talent" != 0
and "Other" != 0;
This result me with count 0
Whereas
select count(*) from web.individual_usage_vw
where "Business & economy" = 0
and "Executive rewards" = 0
and "Health & benefits" = 0
and "Investment" = 0
and "Corporate marketing" = 0
and "Retirement" = 0
and "In General" = 0
and "Mergers & acquisitions" = 0
and "Corporate strategy operations" = 0
and "Broad-based rewards" = 0
and "Leadership" = 0
and "Talent" = 0
and "Other" = 0;
Result me with count = 36228
I am not able to debug this error.
There is nothing wrong with the results. The second query is fetching rows only when all your columns (in the predicate) are having 0. In your first query, you are fetching rows where all columns have non zero values. The missing records (delta) might be having one or more columns where the result is zero.
For example
C1, C2
0 0
1 0
1 1
0 1
select * from t1 where c1 = 0 and c2=0; -- returns one row
select * from t1 where c1 != 0 and c2!=0; -- returns one row and not three
Hence the difference. If you need to return all rows that are not returned in your second query then use NOT EXISTS
Roughly, this is what you need.
SELECT columns
FROM table
WHERE NOT EXISTS (SELECT *
FROM table
WHERE all columns = 0) ;
SELECT TOP 1
CostValue
FROM
[~client_table~].[dbo].[CostRules] AS CostRule
WHERE
(CASE
WHEN DATALENGTH(CostRule.ModelName) = 0
THEN
CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1
ELSE
CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1
END
)
) AS MonoCost
I want to define my where statement depending on the datalength of CostRule.ModelName. But i got an error: Incorrect syntax near '='. in CostRule.Type = 1 and i got a error in the ELSE statement.
Must be like this:
...
WHERE
(DATALENGTH(CostRule.ModelName) = 0
AND CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1)
OR
(DATALENGTH(CostRule.ModelName) != 0
AND CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1)
The CASE-style from your query cannot work.
you can change your statement like this:
SELECT TOP 1
CostValue
FROM
[~client_table~].[dbo].[CostRules] AS CostRule
WHERE CostRule.ColorType=1
AND CostRule.Type=CASE WHEN DATALENGTH(CostRule.ModelName) = 0 THEN 1 ELSE 2 END
AND CostRule.Manufacturer=CASE WHEN DATALENGTH(CostRule.ModelName) = 0 THEN Printer.ManufacturerId ELSE Printer.ModelName END
You can't use a CASE statement to define where conditions like that. It will be easier to just use some boolean logic
SELECT *
FROM your_table
WHERE (DATALENGTH(CostRule.ModelName) = 0
AND CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1)
OR (DATALENGTH(CostRule.ModelName) != 0
AND CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1)
There are some other things that could be removed (like CostRule.ColorType = 1 since it is the same in both branches) but I've left them in here to illustrate how to transform your CASE statement into a boolean logic set.
It looks like you would just need to change the WHERE statement:
It looks like you will just need to change your WHERE statement to use OR and remove the CASE Statement.
(SELECT TOP 1
CostValue
FROM
[~client_table~].[dbo].[CostRules] AS CostRule
WHERE
DATALENGTH(CostRule.ModelName) = 0
CostRule.Type = 1
AND CostRule.Manufacturer = Printer.ManufacturerId
AND CostRule.ColorType = 1
OR
DATALENGTH(CostRule.ModelName) <> 0
AND CostRule.Type = 2
AND CostRule.ModelName = Printer.ModelName
AND CostRule.ColorType = 1
) AS MonoCost
I have 15 items in my ComboBox and when the user selects an item I want to present something different in my TextBox.
At the moment I have:
If cb_dropdown.SelectedIndex = 0 Then
RTB_Sql.Text = "update access
set accessdesc = 'Less than 5' where accessID < '5'"
Else
If cb_dropdown.SelectedIndex = 1 Then
RTB_Sql.Text = "update access
set accessdesc = 'More than 5' where accessID > '5' and < '10' "
Else
If cb_dropdown.SelectedIndex = 2 Then
RTB_Sql.Text = ""
etc....
Is there a nicer and more methodical way to approach this as it looks quite scruffy?
Yes, it is called select.
Select Case cb_dropdown.SelectedIndex
Case 0 To 4
RTB_Sql.Text = "update access
set accessdesc = 'Less than 5' where accessID < '5'"
Case 5
RTB_Sql.Text = [...]
Case Else
RTB_Sql.Text = [...]
End Case
Although in your case I think what you are looking for is < (less than) and > (greater than).
If cb_dropdown.SelectedIndex < 5 Then
RTB_Sql.Text = "update access set accessdesc = 'Less than 5' where accessID < '5'"
ElseIf cb_dropdown.SelectedIndex < 10 Then
RTB_Sql.Text = "update access set accessdesc = 'More than 5' where accessID > '5' and < '10' "
End If
Not really sure what you are trying to do though. Maybe if you explain in more detail someone can provide a better answer. So let me take a wild guess:
Dim n As Integer = cb_dropdown.SelectedIndex * 5
RTB_Sql.Text = "update access set accessdesc = 'More than " + n + "' where accessID > '" + n + "' and < '" + (n+6) + "' "
This will give you the following result based on SelectedIndex:
0 = from 1 to including 5
1 = from 6 to including 10
etc...
If you want to shift it down one (include 0 and not 5 in first batch) then just change > to >= and 6 to 5.
You can also use something like this if you want to check range of values:
Sub Main()
Dim i As Integer = 6
Select Case True
Case 0 <= i AndAlso i < 5
Console.WriteLine("i is between 0 and 4")
Case 6 <= i
Console.WriteLine("i is 6 or greater")
End Select
Console.ReadLine()
End Sub
Be aware that it stops in the first true condition.
I've got this code here and you can see from my Pseudocode what I'm trying to accomplish
select *
from dbo.BenefitsForms
inner join Dependents on BenefitsForms.UserId = Dependents.BenefitsForm_UserId
inner join CoverageLevels on BenefitsForms.MedicalId = CoverageLevels.Id
where (BenefitsForms.MedicalId > 0 AND BenefitsForms.MedicalId < 13)
AND Dependents.IsSpouse = CASE when CoverageLevels.[Level] = 2 then 1
when CoverageLevels.[Level] = 3 then 0 end
when CoverageLevels.[Level] = 4 then [any, it doesnt matter] <--- my desire but it doesn't work.
What can I do to get the effect I desire in the brackets? If Coverage Level = 4 then I don't care what Dependents.IsSpouse is, I don't even need to sort by it anymore.
Assuming that isSpouse can only be 0 or 1... if CoverageLevels.Level is 4, then compare isSpouse to itself, which will always result in true:
AND Dependents.IsSpouse = CASE
when CoverageLevels.[Level] = 2 then 1
when CoverageLevels.[Level] = 3 then 0
when CoverageLevels.[Level] = 4 then Dependents.IsSpouse
END
Alternately, this can also be expressed without the CASE:
WHERE
BenefitsForms.MedicalId > 0
AND BenefitsForms.MedicalId < 13
AND (
(Dependents.IsSpouse = 1 AND CoverageLevels.[Level] = 2)
OR (Dependents.IsSpouse = 0 AND CoverageLevels.[Level] = 3)
OR CoverageLevels.[Level] = 4
)
Is there a way to do this shorter, for instance using some sort of conditional operator in Transact-sql?
IF #ParentBinaryAssetStructureId = -1
BEGIN
SET #ParentBinaryAssetStructureId = NULL
END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = #ParentBinaryAssetStructureId
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
USE NULLIF()
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = NULLIF(#ParentBinaryAssetStructureId,-1)
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
The ternary (conditional) operator in c like languages:
x = doSomething ? 5 : 7
would be written like this in SQL:
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 ELSE 0 END
There can be multiple cases (when clauses):
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 WHEN #somethingElse = 1 THEN 20 ELSE 0 END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId =
CASE ParentBinaryAssetStructureId
WHEN -1 THEN NULL
ELSE ParentBinaryAssetStructureId
END
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
Give that a whirl