SET with case when SQL - sql

I'm trying to update the status column of this table and I used the following:
UPDATE table_a
SET status = CASE
WHEN test_group LIKE 'Control' THEN 0
WHEN test_group NOT LIKE 'Control' THEN 1
END;
But it doesn't appear to have worked, it actually just gave every observation a 1 for status. I just want the observations that have the word "control" in them to take a 0 for status and observations that don't have the word "control" in them to take a 1. The default was not a 0 or 1 for this column.

You don't need the second condition. But you do need wildcards for the LIKE. I would suggest:
UPDATE table_a
SET status = (CASE WHEN test_group LIKE '%Control%' THEN 0 ELSE 1 END);
In many databases, you can simplify this to:
UPDATE table_a
SET status = (test_group NOT LIKE '%Control%');

Try this (T-SQL):
UPDATE table_a
SET status = CASE
WHEN charindex('Control', test_group)>0 THEN 0
ELSE 1
END;

UPDATE table_a
SET status = CASE
WHEN test_group LIKE '%Control%' THEN 0
WHEN test_group NOT LIKE '%Control%' THEN 1
END;

Related

Can a CASE expression have 2 resultant values

I have to write a case expression in SQL which goes like this,
case condition
if (T_CD = 'Y')
Case C_CD = 'H3'
set R_ID = 3 and RS_ID = 25
CASE A_FLG = 'N' and Mod = 'D'
set R_ID = 3 and RS_ID = 31
Both R_ID and RS_ID populate columns in a different table and have to be derived as per condition above.
My question is - Since I want 2 separate fields out of my case expression, will a single Case give out 2 resultant field values for me. Or Do I have to write 2 different case expressions for it.
If your dbms supports row types, maybe this works for you:
select case when a = 1 then (1,2) else (3,4) end from testtable;
The SQL Validator says:
The following feature outside Core SQL-2003 is used:
T051, "Row types"

SQL Indicate Completion once 2 matching result queries are matched

I have 2 queries. I basically need to work out from the 2 queries when the all threads have completed processing and alert in a form of Print Complete. To do this - If the current Processing query matches total put through query output number, All threads are completed in that batch. I need to create a stored proc that would PRINT Complete. What would be the best way of doing this?
--How Many To Process - Which is put through first (but not processed)
SELECT COUNT(IsProcessed) as 'Current Put through'
FROM [dbo].[threads]
-- Current Processed -- This incrementally goes up by the Isprocessed flag changing 1 by 1 to the value of 1
SELECT COUNT(IsProcessed) 'Current_Processing'
FROM [dbo].[threads] as count
where IsProcessed=1
Can you do something like the following?
IF NOT EXISTS (SELECT 1 FROM dbo.Threads WHERE IsProcessed != 1)
BEGIN
PRINT 'Complete'
END
ELSE
BEGIN
PRINT 'Ongoing'
END
select *,
case when cntAll = cntProcessed then 'Complete' else 'Ongoing' end as Status
from (
select count(case when IsProcessed = 1 then 1 else NULL end) Current_Processing,
count(1) 'Current Put through'
from [dbo].[threads])A

Completing the where clause in sql server

I have a table kyc3 where there are walletno, status and rank columns present. rank columns are currently filled with 0. While status column has following data: accepted, rejected, registered and scanned. I want to put value for each status in rank column where
accepted = 1, rejected = 2, registered = 3 and scanned = 4
I wrote following query but do not understand how to complete it:
INSERT INTO kyc3([rank]) SELECT status_ FROM kyc3
I understand I need to put a where clause that will indicate my logic for data population. But what should I write?
If the table is populated and you want to change the rank field, you want to use an UPDATE statement, as INSERT is for adding new rows to your table:
UPDATE kyc3
SET rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
You can use update to fill a cell of an existing row.
update kyc3
set rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
Use insert only for creating new rows.

case compare to number and give out name

the problem I have is that I have a column called cate with the numbers 1 to 5 but I want alias names in the print out.
For example if the column has the number 1 I want STONE in the result set, if it is 2 I want "TREE".
I should look something like
Select
case when t.cate = 1 then t.cate="STONE"
case when t.cate = 2 then t.cate="TREE"
else null end as test from dbt.tbl t
I do not want to change the value in the table only in the print out.
Any idea how I can that to work?
Thanks for all your help in advance
remove extra case,
SELECT CASE WHEN t.cate = 1 THEN 'STONE'
WHEN t.cate = 2 THEN 'TREE'
ELSE null
END AS test
FROM dbt.tbl t
Alternatively, you can write
SELECT
CASE t.cate
WHEN 1 THEN 'STONE'
WHEN 2 THEN 'TREE'
ELSE NULL
END AS test
FROM dbt.tbl t
If the list is likely to change in the future (either through edits or additions), I'd do it as a separate table:
INSERT INTO Cates (Cate,Description) VALUES
(1,'Stone'),
(2,'Tree') --Etc
And then just do:
SELECT c.Description as Test
FROM dbt.tbl t inner join Cates c on t.Cate = c.Cate

Modifying a column value depending on other column value for all the rows

I have a teradata table. Now I need to add a column, say, Flag and insert values into the Flag column which will depend on say, Sales. Flag=1 if Sales greater than x or Flag=0.
Here is the structure of Table at present
Sales Date
10 11/11/1987
20 12/13/1987
I want it like the following way
Sale Date Flag
10 11/11/1987 0
20 12/13/1987 1
I tried to look for such problems on the forums but couldn't find any. Excuse if you find any duplicate problems.
What you want to use here is a CASE statement:
UPDATE teradata_table
SET flag = CASE WHEN sales > 10 THEN 1 ELSE 0 END;
after adding the column, do the update statement
Update <table>
set Flag = case when Sale<=10 then 0
else 1
end
ALTER TABLE MYTABLE
( FLAG NUMBER(1) );
UPDATE MYTABLE SET FLAG = 1 WHERE SALE >= 10;