I have a table with following columns :
SalaryStructure:
Id StructureName IsApplicable IsActive
IsApplicable - bit, not null
"IsApplicable" is a newly added column which has all the values as "false" by default.
Now, I want to update "IsApplicable" based on pattern matching logic and then set the value of "IsApplicable" based on that.
Query:
SELECT
CASE
WHEN (StructureName LIKE '%Associate1%' OR StructureName Like '%Tier1%')
THEN 'No' ELSE 'Yes' END AS IsApplicable,
FROM SalaryStructure
WHERE IsActive = 0
I want to run above script on same table SalaryStructure and update the values "Yes" and "No" in the column "IsApplicable".
But I am not getting how to include this part in "Update" statement. I want update all the records in the SalaryStructure table.
Can someone please help me?
Neither 'Yes' or 'No' are valid bit values, a non-NULLable bit column can store 1 or 0 and that is it. I therefore assume 'No' should be 0 and 'Yes' should be 1.
As for the UPDATE it would, in truth, look like another other UPDATE on a single table. UPDATE...SET...WHERE:
UPDATE dbo.SalaryStructure
SET IsApplicable = CASE WHEN (StructureName LIKE '%Associate1%' OR StructureName LIKE '%Tier1%') THEN 0
ELSE 1
END
WHERE IsActive = 0;
Related
I'm beginner in SQL,is it possible to change column name in CASE statement.
I've a column name as "Inbound" the value for it can be '1' or '2'. If it's 1 the column name should be AS "IN" ELSE "OUT".
Any help is greatly appreciated.
You can not generate dynamic column names. Queries should be static. But what you could do is for instance:
select case when inbound = 1 then 'yes' end as in_result,
case when inbound = 2 then 'yes' end as out_result
from your_table
I have condition in datastage which verifies 30 columns as not null and assigning value as '0' if anyone of the field is null like below,
isnull(columnA) or isNull(coloumnB) or isnull(columnC) or isNull(coloumnD) then 0 else 1.
we have option to use case statement in hive to set values, however we have to give use CASE statement per column like below,
select
case when (columnA is NULL) then 0
case when (columnB is NULL) then 0
case when (columnC is NULL) then 0
.
.
.
else 1
end as iValidColumn
From tablea
What i am looking?
Trying to look for option where we can validate all the 30 columns in condition for null validation.
The task is creating sql script which set flag "Y" or "N" to existed dummy database line "Status" if integer value more or less '10'
database consists of three columns
person
money (5 or 100)
status (dummy - should be updated via stored procedure)
Solution is
update status set flag = "Y" where money = '5' and
status set flag = "N" where money = '100'
Novice in SQL scripting
UPDATE
people
SET
status = CASE WHEN money = '5' THEN 'Y'
WHEN money = '100' THEN 'N'
ELSE status
END
WHERE
money IN ('5', '100')
Working backwards through that query...
The WHERE clause ensures that only the rows you want to update are updated.
The CASE statement then chooses which value to set status to based on the existing value in money.
The ELSE block should never happen, but is there as a fail-safe (in-case you forget to include the WHERE clause when you run it, the value of status gets set to the current value in status for any rows where money is neither '5' or '100').
EDIT :
Notes:
people should be the name of the table
status should be the name of the field
Final Edit (I give up now) :
UPDATE
people
SET
status = CASE WHEN money > 10 THEN 'Y' ELSE 'N' END
;
This will update every row in people.
It will set status to 'Y' if money > 10, for all other records it will set status to 'N'.
How about using a CASE
Evaluates a list of conditions and returns one of multiple possible
result expressions.
The CASE expression has two formats:
• The simple CASE expression
compares an expression to a set of simple expressions to determine the
result.
• The searched CASE expression evaluates a set of Boolean expressions
to determine the result.
update [status]
set [flag] =
CASE
WHEN [money] = '5' THEN 'Y'
WHEN [money] = '100' THEN 'N'
END
WHERE [money] in ('5','100')
I was wondering if we can change the label of the select statement like we do for data in sql select using CASE
SELECT CASE column1 = 1 THEN 1 ELSE 0 END AS [Available]
But can we have a dynamic header something like
SELECT column1 AS <-- Available when 1 or Not Available when 0
This can be handled on the front end but its wise if we have it on backend. Any help or useful link is appreciated
You can do it with dynamic sql and if...else instruction but it not make sense for me. In relational database value in the cell tells you if something is available or not. If header tells you the same as cell it's duplicate information. If you want to description of the value you can use case syntax instead of 0/1 value
SELECT CASE when column1 = 1 THEN 'Available'
ELSE 'Not available'
END AS [Available]
Well, that would not make sense, as what would you expect the column name to be if you hade 2 rows, one with 1 (being available) and the other with 0(being Not Available)?
You would have to stick to something like
SELECT
CASE
WHEN column1 = 1
THEN 'Available'
ELSE 'Not available'
END as Availability
FROM YourTable
I am working on a select statement in SQL and am running into issues trying to create a where clause that includes a case statement or an if else statement. I want to select records based on the value of a variable. If the variable is 'True' then only return records from the select statement where a column is null. If the variable is not 'True' then return all records regardless if that columns is null.
Any tips on how to do this?
Below is a simple example of what i am trying to do:
declare #option1 as varchar(5)
--This can be True or False so to test i just put the Set option below
set #option1 = 'True'
Select a,b,c,d...
from ...
where d = case when #option1 = 'True' then NULL End
This is the part where i do not know what to do. I only need to filter out the records if the variable is 'True' so not sure what to put in the else section of the case.
You can't test for d = NULL as your CASE statement does because that will always return false since NULL is not equal to NULL (unless you set ANSI_NULLS to 'off').
The simplest thing to do would be to change the WHERE clause to this:
WHERE #option1 = 'False' OR d IS NULL
If you prefer to use a CASE statement for some reason, you can write it like this:
WHERE 1 = CASE WHEN #option1 = 'False' THEN 1
WHEN #option1 = 'True' AND d IS NULL THEN 1
ELSE 0
END
This:
UPDATE: PinnyM has straightened me out on this. I am leaving my embarrassing logically flawed argument here for the education of the masses. The solution I propose below after "Try this" is certainly still valid, but PinnyM's solutions is by far more elegant and should be used.
WHERE #option1 = 'False' OR d IS NULL
Will always return all the results given his current select statement (assuming #Option1 is simply a flag parameter passed in).
Try this:
SELECT a, b, c, d
WHERE
-- Returns only rows where d is null (if #Option1 is True)
(#Option1 = 'True' AND d IS NULL)
OR
-- returns all the rows (if #Option1 is False)
(#Option1 = 'False')