I have some values like this in the database with three records
id
TEST_TEST1
TEST_TEST2
TEST_TEST3
Now i need to append all the values with a "PREFIX". So it becomes PREFIX_TEST_TEST1, PREFIX_TEST_TEST2 etc. But for the third value TEST_TEST3, I have to change it to PREFIX_TESTTEST3 (no underscore)
So i made it using a two update queries like below
update table set id=concat('PREFIX',id) where id in ('TEST_TEST1','TEST_TEST2');
and the second update statement
update table set id='PREFIX_TESTTEST3' where id='TEST_TEST3'
Is there any way we can make both these updates in one update statement?
CASE expression helps.
SQL> update test set
id = 'PREFIX_' || case when id = 'TEST_TEST3' then replace(id, '_')
else id
end
where id in ('TEST_TEST1','TEST_TEST2','TEST_TEST3');
3 rows updated.
SQL> select * From test;
ID
------------------------------
PREFIX_TEST_TEST1
PREFIX_TEST_TEST2
PREFIX_TESTTEST3
SQL>
You can use a case expression, for example:
update table
set id = case
when id in ('TEST_TEST1','TEST_TEST2' ) then concat('PREFIX',id)
when id ='TEST_TEST3' then 'PREFIX_TESTTEST3'
end
where id in ('TEST_TEST1','TEST_TEST2','TEST_TEST3')
You can also decode function to do that
update Your_table
set id = 'PREFIX_' || decode( id, 'TEST_TEST3', replace(id, '_', ''), id )
where id in ('TEST_TEST1', 'TEST_TEST2', 'TEST_TEST3')
;
Related
I have a historic table that won't be updated with new inserts, and a have a view that everyday will be update with new inserts. So I need to know if my SQL is correct.
This SQL needs to get all entries that inside in FATO_Proposta_Planilha table (Table 1)
and to add the entries not similar that are in the FATO_Proposta_View table (Table 2).
So, this SQL must have all entries from Table 1 more all entries from Table 2 that are not repeated in the Table 1. Can you give a opinion about this SQL, please?
SELECT vw.[DescPac] [PA]
,vw.[DescRegional] [Regional]
,vw.[DescSuperintendencia] [Superintendencia]
,vw.[NUM_CPF_CNPJ] [Documento_Numero]
,pla.[Nome] [Nome]
,pla.[Produto] [Produto]
,pla.[Modalidade] [Modalidade]
,vw.[NUM_CONTRATO_CREDITO] [Contrato]
,vw.[DESC_FINALIDADE_OPCRED] [Finalidade]
,vw.[DATA_OPERACAO] [Data_operacao]
,pla.[Data_mov_entrada] [Data_mov_entrada]
,vw.[DATA_VENC_OPCRED] [Data_vencimento]
,vw.[VALOR_CONTRATO_OPCRED] [Valor_contrato]
,pla.[Processo_Lecon] [Processo_Lecon]
,CASE WHEN ISNULL(pla.Origem, '') = ''
THEN 'Esteira Convencional'
ELSE pla.Origem
END [Origem]
FROM Proposta_View vw
LEFT JOIN FATO_Proposta_Planilha pla
ON vw.NUM_CONTRATO_CREDITO = pla.Contrato
UNION
SELECT [PA] [PA]
,[Regional] [Regional]
,[Superintendencia] [Superintendencia]
,[Documento_Numero] [Documento_Numero]
,[Nome] [Nome]
,[Produto] [Produto]
,[Modalidade] [Modalidade]
,[Contrato] [Contrato]
,[Finalidade] [Finalidade]
,[Data_operacao] [Data_operacao]
,[Data_mov_entrada] [Data_mov_entrada]
,[Data_vencimento] [Data_vencimento]
,[Valor_contrato] [Valor_contrato]
,[Processo_Lecon] [Processo_Lecon]
,CASE WHEN ISNULL(Origem, '') = ''
THEN 'Esteira Convencional'
ELSE Origem
END [Origem]
If you are only inserting rows through the view you can add an extra column with a DEFAULT value to distinguish the old rows from the new ones.
For example if you have a table t as:
create table t (a int primary key not null);
insert into t (a) values (123), (456);
You can add the extra column as:
alter table t add is_new int default 1;
update t set is_new = 0;
create view v as select a from t;
Then each insert through the view won't see that new column and will insert with value 1.
insert into v (a) values (789), (444);
Then it's easy to find the new rows:
select * from t where is_new = 1;
Result:
a is_new
---- ------
444 1
789 1
Se running example at db<>fiddle.
I have a table in PostgreSQL which has a column names "value" that can accept two string values (lets say "one" and "two")
I would like to create a query that updates a row and if value column is currently "one" it updates it to be "two" and vice-versa
I've tried to the the following query:
IF EXISTS(SELECT * FROM table_name WHERE filename='file1' and value='one')
THEN UPDATE table_name SET value='two' WHERE filename='file1'
ELSE
UPDATE table_name SET value='one' WHERE filename='file1'
end if;
But I keep getting "syntax error at or near "IF""
Any help please?
You wouldn't use if for this. You would use case:
update table_name
set value = (case when value = 'one' then 'two' else 'one' end);
Or, if you choose to represent the binary value as a boolean, this is simpler with not:
update table_name
set value = not value;
I have a postgres table defined as below:
CREATE TABLE public.Table_1
(
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1
START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 )
)
Due to data migration, the id column is messed up and the value for id that is being generated on INSERT is not unique. Hence, I need to reset the id column as below
SELECT MAX(id) + 1 From Table_1;
ALTER TABLE Table_1 ALTER COLUMN id RESTART WITH 935074;
Right now I run the first query to get the Max(id) + 1 value and then I need to substitute it in the ALTER query.
Is there a way to store the result of SELECT and just use the variable inside ALTER statement?
Here is one way to do it:
select setval(pg_get_serial_sequence('Table_1', 'id'), coalesce(max(id),0) + 1, false)
from Table_1;
Rationale:
pg_get_serial_sequence() returns the name of the sequence for the given table and column
set_val() can be used to reset the sequence
this can be wrapped in a select that gives you the current maximum value of id in the table (or 1 if the table is empty)
I've got a table with two columns, ID and Value. I want to change a part of some strings in the second column.
Example of Table:
ID Value
---------------------------------
1 c:\temp\123\abc\111
2 c:\temp\123\abc\222
3 c:\temp\123\abc\333
4 c:\temp\123\abc\444
Now the 123\ in the Value string is not needed. I tried UPDATE and REPLACE:
UPDATE dbo.xxx
SET Value = REPLACE(Value, '%123%', '')
WHERE ID <= 4
When I execute the script SQL Server does not report an error, but it does not update anything either. Why is that?
You don't need wildcards in the REPLACE - it just finds the string you enter for the second argument, so the following should work:
UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <=4
If the column to replace is type text or ntext you need to cast it to nvarchar
UPDATE dbo.xxx
SET Value = REPLACE(CAST(Value as nVarchar(4000)), '123', '')
WHERE ID <=4
Try to remove % chars as below
UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <=4
To make the query run faster in big tables where not every line needs to be updated, you can also choose to only update rows that will be modified:
UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <= 4
AND Value LIKE '%123%'
query:
UPDATE tablename
SET field_name = REPLACE(field_name , 'oldstring', 'newstring')
WHERE field_name LIKE ('oldstring%');
You have one table where you have date Code which is seven character something like
"32-1000"
Now you want to replace all
"32-"
With
"14-"
The SQL query you have to run is
Update Products Set Code = replace(Code, '32-', '14-') Where ...(Put your where statement in here)
For anyone want to replace your script.
update dbo.[TABLE_NAME] set COLUMN_NAME= replace(COLUMN_NAME, 'old_value', 'new_value') where COLUMN_NAME like %CONDITION%
CREATE TABLE tbl_PersonalDetail
(ID INT IDENTITY ,[Date] nvarchar(20), Name nvarchar(20), GenderID int);
INSERT INTO Tbl_PersonalDetail VALUES(N'18-4-2015', N'Monay', 2),
(N'31-3-2015', N'Monay', 2),
(N'28-12-2015', N'Monay', 2),
(N'19-4-2015', N'Monay', 2)
DECLARE #Date Nvarchar(200)
SET #Date = (SELECT [Date] FROM Tbl_PersonalDetail WHERE ID = 2)
Update Tbl_PersonalDetail SET [Date] = (REPLACE(#Date , '-','/')) WHERE ID = 2
you should use the below update query
UPDATE dbo.xxx SET Value=REPLACE(Value,'123\','') WHERE Id IN(1, 2, 3, 4)
UPDATE dbo.xxx SET Value=REPLACE(Value,'123\','') WHERE Id <= 4
Either of the above queries should work.
replace for persian word
UPDATE dbo.TblNews
SET keyWords = REPLACE(keyWords, '-', N'،')
help:
dbo.TblNews -- table name
keyWords -- fild name
I am looking to replace values in a particular column. For example the following column values
column name
----------
Test1
Test2
Test3
Test12
should be (replacing est1 with rest1)
column name
----------
Trest1
Test2
Test3
Trest12
Use REPLACE:
SELECT REPLACE(t.column, 'est1', 'rest1')
FROM MY_TABLE t
If you want to update the values in the table, use:
UPDATE MY_TABLE t
SET column = REPLACE(t.column, 'est1', 'rest1')
If you need to update the value in a particular table:
UPDATE TABLE-NAME SET COLUMN-NAME = REPLACE(TABLE-NAME.COLUMN-NAME, 'STRING-TO-REPLACE', 'REPLACEMENT-STRING');
where
TABLE-NAME - The name of the table being updated
COLUMN-NAME - The name of the column being updated
STRING-TO-REPLACE - The value to replace
REPLACEMENT-STRING - The replacement
In Oracle, there is the concept of schema name, so try using this
update schemname.tablename t
set t.columnname = replace(t.columnname, t.oldvalue, t.newvalue);
I'm using Version 4.0.2.15 with Build 15.21
For me I needed this:
UPDATE table_name SET column_name = REPLACE(column_name,"search str","replace str");
Putting t.column_name in the first argument of replace did not work.