Set flag to database column - sql

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

Related

How to update column based on select statement result

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;

Case statement in where clause using not like and is not null

I'm pulling data from an existing table using a stored procedure that has some yes or no choices that the user picks on the front end through a checkbox. I want to limit writing a bunch of different If statements for every choice they make.
This portion of my where clause works. Data is either Y or N for this column.
Where... and IsSigned = Case When #IncludeSigned = 'Y' then IsSigned else 'N' end
I would like to add to the where using is not null and not like if this is possible between the square brackets. So far I have
and SignatureType = case when #IncludeElectronic = 'Y' then Type else [NOT like electronic] end
also
and ReviewDate = Case When #HasReviewDate = 'Y' then [ReviewDate is not null] else null end
This may help you use AND/OR instead of case
where (ReviewDate is not null or #HasReviewDate = 'Y' ) And (....)
ie when #HasReviewDate = 'Y' query will return the records with ReviewDate is not null
and when #HasReviewDate != 'Y' then query will return the records with ReviewDate is null
think of it this way:-
Your first case statement has two possible results:-
IsSigned = 'Y'
IsSigned = 'N'
Your subsequent ones have problems as they don't make sense syntactically. So the second one as written returns
SignatureType = Type
SignatureType = [NOT like electronic]
and your third:
ReviewDate = [ReviewDate is not null]
ReviewDate = null end
SO the operator has to be before the case statement and apply to all of the results of the case statement.
For example
WHERE myfield not like CASE WHEN thatfield=1 THEN 'Fish' ELSE 'Chips END
would produce either
myfield not like 'Fish'
myfield not like 'Chips'
I believe you can not use in this way, apart of that, the impact that is not like can have inside your query can be high, my recommendation changes the strategy that you are using.

Case inside 'where' section that changes the condition of an AND statement

I am creating a store procedure and i am wondering how can i add a case block in an Add statement inside the where statement.That case statement checks an input parameter and depending its value it will change the condition from greater that to smaller than and of course be added to the add conditions
So a part of the query is like:
WHERE
AND BM.Example1 IS NOT NULL
AND BM.Example2 IS NOT NULL
AND ( Case When #inputParamter= 'A' THEN AND BM.Example < 0 ELSE And BM.Example> 0 )
ORDER BY 'SEG' ASC, 'CCY' ASC
So by this approach i am thinking to extract an add statement depending on the input parameter but unfortunately i keep getting syntax errors.
Is that possible?
Yepp, just use this:
AND (( #inputParamter= 'A' AND BM.Example < 0) OR ( #inputParamter<>'A' AND BM.Example> 0) )
However, be carefull with NULL, you have to put it in the logic as a third option.
here is a similar answer using case
AND ( Case When #inputParamter = 'A' AND BM.Example < 0 THEN 'Y'
When #inputParamter <> 'A' AND BM.Example > 0 THEN 'Y' ELSE 'N' END = 'Y')

String data right truncation DB2 error

I am receiving the error "String data right truncation" on db2 when I use this query
SELECT BILL_NUMBER, 'PAPERWORK BUT NOT COMPLETE', 'NONE', NULL, '00000',NULL,NULL,TOTAL_CHARGES, NULL FROM TLORDER WHERE
CURRENT_STATUS NOT IN ('COMPLETE','APPRVD','PAPERWISE','BILLD','EDIBILLED','CANCL') AND BILL_TO_CODE NOT LIKE CASE WHEN :INCLUDE_DED = 'No' THEN 'ROCD%' ELSE '1234kkh5656' END
AND EXISTS (SELECT 1 FROM LIST_CHECKIN_AUDIT A WHERE A.BILL_NUMBER = TLORDER.BILL_NUMBER FETCH FIRST 1 ROW ONLY)
AND SITE_ID = :SITE AND DELIVER_BY_END >= CURRENT TIMESTAMP - 3 MONTHS AND COALESCE(PICK_UP_DRIVER,'') = '' AND '00000' =:DRIVER_ID
However when I suppress this line I do not get the error.
AND BILL_TO_CODE NOT LIKE CASE WHEN :INCLUDE_DED = 'No' THEN 'ROCD%' ELSE '1234kkh5656' END
Thanks in advance!
I'd venture to guess that this happens when the value of the :INCLUDE_DED host variable exceeds 2 bytes in length. You do not supply the variable data type, so the query compiler derives it from the right side of the comparison, where the literal 'No' has the length of 2 bytes. If you then assign a value like 'Yes' to the host variable it has to be truncated.
Consider adding an explicit type information to the host variable reference, e.g.:
...WHEN CAST(:INCLUDE_DED AS VARCHAR(10)) = 'No'...
Use the data type appropriate for the range of possible values.
I would first check the datatype of the bill_to_code. You are returning '1234kkh5656' that may exceed the length of the datatype.

Using SQL to return 6 check box flags into three columns

I am trying to figure out how to turn multiple check box results in differnet fileds into seperate columns.
The current case statement below only tracked the lowest score into a a single filed called 'Activities Registered For (1) – (5)'. I would like to convert them into 5 columns 'a-e' where 'a' is always filled with a result, and if two options are checked the results are in 'a' and 'b'. The form can be filled in with up to all selections checked. The else statement appears to be an error, since there are to be at least one of the five boxes checked.
I am new to SQL and I adopted this from someone else, so I am sorry for not showing my previous attempts to resolve my issue.
,CASE
WHEN [1524#1] = 'Y' THEN '1'
WHEN [1525#1] = 'Y' THEN '2'
WHEN [1526#1] = 'Y' THEN '3'
WHEN [1527#1] = 'Y' THEN '4'
WHEN [1528#1] = 'Y' THEN '5'
ELSE ' ' END AS 'Activities Registered For (1) – (5)'
You could use a PIVOT but multiple CASEs are just as effective, use about the same amount of code and is easier for beginners to decipher.
CASE WHEN [1524#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_1,
CASE WHEN [1525#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_2,
CASE WHEN [1526#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_3,
CASE WHEN [1527#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_4,
CASE WHEN [1528#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_5
I don't think the ELSE is necessarily an error. I often add an ELSE than shouldn't be used in case there is unexpected data (no Y in any field in your example).
Here's some info on PIVOTs if you want to check it out:
http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query