How to change data value in a column if it match with flag=1 - sql

So assume there are 2 cols Name and Flag. I want to change the name of the data which have flag=1.
This is the table (https://i.stack.imgur.com/LILzV.png)
And this is what i want.(https://i.stack.imgur.com/JPZWj.png)
I have first created a flag according to some condition. Now im not able to fig out the next move.

update TableName
set name = 'zumba'
where flag = 1
Edit: as pointed out by jarlh, single quotes are the correct syntax

Related

How can I replace the first two characters of every row in a specific column using SQL?

I'm working with a table. In the table there is a column called ticket number which contains several rows of data. All of the values in the row begin with J2. I'd like to change the first two characters of all the rows to A3. How can I use SQL to do this. I'm familiar with the replace function:
SELECT REPLACE ([ticket number],'J2','A3')
But clearly the example above will not work, since it will change all of the J2 occurrences to A3 while I need to replace the first one at the beginning of ticket number. Any help would be appreciated.
Ticket Number
J2F4T45T
J2J3J3J2
J25TGYHJ2
J2FFJ2J2
J2MG8NGJ2
The desired result should be:
Ticket Number
A3F4T45T
A3J3J3J2
A35TGYHJ2
A3FFJ2J2
A3MG8NGJ2
Not sure if this is what you are looking for. But you could try to use a right function to get all but the 1st two character from the ticket_number:
SELECT 'A3' + RIGHT(ticket_number ,len(ticket_number)-2)
And if you need to update the table you could try something like this:
UPDATE ticket
set ticket_number = 'A3' + RIGHT(ticket_number ,len(ticket_number)-2)
db fiddle
This is also another method:
UPDATE ticket
set ticket_number = STUFF(ticket_number,1,2,'A3')

postgresql remove last 4 characters from text values

I have a table with only one column that has multiple text values. All of them end with same 4 characters that I would like to remove.
Could anyone, please, help me with a query for that?
I've already tried 'replace'
SELECT REPLACE(ticker, 'USDT', '') FROM tickers;
It appears to do what I want, but it doesn't update my data in a table.
You need an update statement
UPDATE tickers
SET ticker = REPLACE(ticker, 'USDT', '');
If you are not sure about the last 4 character, you can update using below as well -
UPDATE tickers
SET ticker = SUBSTR(ticker,-4);

How to Find and replace value in Access table using VBA

I have a table in access in which I want to find and replace a cell value using VBA code.
the value i want to find is an int and the value i want to replace it with is also an int
Any tips?
what i have tried so far only returns somethng like: "cant have duplicate value in cells"
The best option to find and replace a value is by using an UPDATE query. You can update a data cell by searching for a given value in that cell, or by searching for a given value in a different cell. The syntax would be along the lines of:
UPDATE MyTableName SET MyFieldName = 1 WHERE MyFieldName = 2
Or:
UPDATE MyTableName SET MyFieldName = 1 WHERE MyOtherFieldName = "Monday"

SELECT a cell and UPDATE at the same time?

I've got a SQL Server table in which I have a column where I would like to select the current value and increment by one, is there a way to do this in a single query? This in order to mitigate the chance, however small it might be, that someone else gets the same number.
Something along the lines of this pseudo code:
SELECT NumSeriesCurrent
FROM NumSeries
(UPDATE NumSeries SET NumSeriesCurrent = NumSeriesCurrent+1)
WHERE NumSeriesKey='X'
To update the value and get the value in NumSeriesCurrent previous to the update you can use
UPDATE NumSeries
SET NumSeriesCurrent += 1
OUTPUT DELETED.NumSeriesCurrent
WHERE NumSeriesKey='X'

insert substring into new column

I have a db where one column contains 2 pieces of data, e.g. first and last name.
The format is roughly ABC-1D23-4F34
I want to copy and insert the first 3 letters, the ABC, into a new column. Lets call these columns [full_id] and [ref_id]
From reading it looks like substring is able to do this but I am doing something wrong here.
INSERT INTO [ref_id]
SUBSTRING([full_id], 1, 3)
FROM db.Name
Thank you for the help.
EDIT:
The update string worked. But I found that there are issues with my data and it is not all in proper formatting.
Is there a way to write a case where if the substring is not 3 letters it writes a null value?
Thanks again, and sorry for having bad data.
Try
UPDATE Name
SET ref_id = CASE WHEN CHARINDEX('-',full_id) = 4 THEN SUBSTRING(full_id,1,3) ELSE NULL END
That will set the ref_id column for all rows using the first 3 characters of the full_id column.
If it is a column in the same table you need to switch to an update statement.
UPDATE db.Name SET [ref_id] = SUBSTRING([full_id], 1, 3)
Perhaps you want something like:
Update db.Name set ref_id = substring([full_id], 1,3)