sql server query for update rows which are multiple of 2 - sql

I've a table with many rows. I've to meet the requirement that those rows which are multiple of 2 should only be updated. e.g.
update [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL]
set remarks = 'multiple of TWO'
--- update only those rows which are multiple of 2.
--- where ID = MULTIPLE OF 2
here ID column is primary key with auto increment
How can I solve this?

You can use modulo as #jarlh said, here is the code:
UPDATE T SET T.remarks = 'multiple of TWO'
FROM [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL] AS T
WHERE ID % 2 = 0

update [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL] set remarks = 'multiple of TWO'where ID % 2 = 0

Related

Add string to existing row separated by comma in sql

id value
1 a
2 b
3 c
How do i add second value 'z' to id=1 (separated by comma)?
id value
1 a,z
2 b
3 c
and how to remove the 'z' now if i have that final table?
You can use update:
update t
set value = concat(value, ',z')
where id = 1;
To answer your secondary question, yes.
If you run Select value from table where id = 1 it will return a,z. that means that if you are going to use it again in queries, you will quite possibly need to utilize a Split() type function, dependent on what you're doing with it.
The best and simplest way to do this is the following query according to me :
update table1 set value = concat(value,'z') where id = 1
where : Table1 is the name of your table.

UPDATE with multiple values from subquery

Suppose I have a table, items, with an integer column named priority, which I'm trying to update from another table. (This other table is a temporary table, where I've pre-calculated all of the appopriate priority values to be applied.)
UPDATE "items" SET "priority" = (
SELECT "newPriority" FROM "newPriorities"
);
What ends up happening is that all entries in items have their priority set to the first value returned from the newPriorities sub-query.
How can I set priority to be different for each record? To put it another way, how can I update items to have all of the correct priority values from the newPriorities table?
Sample Data
items
id priority /* some other, unreleated columns */
2108f97e-e1ce-47bf-97fd-c20699d2aa27 0
fae2347c-8644-47ba-931f-3d3cf70d3565 1
bd5ed046-47fa-49d9-9b40-2aa920511cf2 3
1fc57417-93e1-4382-8246-c4f9d117a55a 4
2ab4afbc-aa56-45af-8509-a7d9377e689d 5
7407a3a5-d410-4190-81c8-54d672f22c8d 6
1b21b57e-e907-4c25-af00-94bbf941df63 7
newPriorities
id newPriority
2108f97e-e1ce-47bf-97fd-c20699d2aa27 15
fae2347c-8644-47ba-931f-3d3cf70d3565 22
bd5ed046-47fa-49d9-9b40-2aa920511cf2 554
1fc57417-93e1-4382-8246-c4f9d117a55a 8
2ab4afbc-aa56-45af-8509-a7d9377e689d 3
7407a3a5-d410-4190-81c8-54d672f22c8d 6
1b21b57e-e907-4c25-af00-94bbf941df63 743
Desired Result: newPriority column values copied to items.
You seem to want:
UPDATE "items" as i
SET "priority" = (SELECT "newPriority"
FROM "newPriorities" np
WHERE np.id = i.id
);

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.

SQL Server find and replace specific word in all rows of specific column

I have a table TblKit that has columns Id and Number. Id is primary key of type int and Number is varchar(50).
The data in the table looks like this:
Id Number
--- ------
1 KIT001
2 KIT002
3 DMB001
4 DM002
5 KIT003
I want to replace all the rows of KIT% with CH in the Number field. The desired output would look like:
Id Number
--- ------
1 CH001
2 CH002
3 DMB001
4 DM002
5 CH003
I have tried this update query:
UPDATE TblKit SET Number = REPLACE(Number, N'%KIT%', 'CH')
But it is not working.
Can anyone help me regarding this?
UPDATE tblKit
SET number = REPLACE(number, 'KIT', 'CH')
WHERE number like 'KIT%'
SQLFiddle Demo
or simply this if you are sure that you have no values like this CKIT002
UPDATE tblKit
SET number = REPLACE(number, 'KIT', 'CH')
SQLFiddle Demo

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;