I want to use select query in the update query statement - sql

I have changed three columns by using a replace command in sql server with following query
select name ,replace (name,'...','.') as names from dbo.emp_2 where name like '%..%'
After replacing the replaced values has been entered in a new row .
Now i just wants to update the replaced values into original table . SO, I was thinking to use select query in the update query .
Is that possible , Is it so kindly provide a sample query for that

Try something like this
Update dbo.emp_2
set name =replace (name,'...','.') where name like '%..%'

Update dbo.emp_2
Set name = replace (name,'...','.')
Where name like '%..%'

You can do:
UPDATE TableA
SET Column1 = 'Test' + Column1
You can even make joins in your update query
UPDATE A
SET A.Column1 = 'Test' + B.Column2
FROM TableA A
INNER JOIN TableB B
ON B.B_Id = A.A_Id

Related

How to update some part of text using SQL UPDATE statement

I have a query saved in column of a table named sqlcode which is maintaining the metadata. For instance following is a query:
select
id, name, address, phone,
.......
......
, upddate
from
bank a
join
bankreg b on a.id = b.id
where
(condition)
I'm trying to update the text of query saved in metadata but I want to only update column names part of the query without making any change to select, from, joins or any other conditions. Any way of only updating column names would be helpful. I've tried using replace function but couldn't got desired output.
I want to update columns of this textual query with the columns present in master table. If new columns are added in master, I want to add them to metadata query as well which is saved in text form. INFORMATION_SCHEMA.COLUMNS gives me the column names now I just want to update these column names to above query
If you wrap the columns you need to update in brackets, you can update them in your REPLACE, without updating the items in the SELECT portion.
select
id,name,address,phone,
.......
......
,upddate
from bank a
join bankreg b
on
a.id = b.id
where ([address] = 'My Street')
Your update would then be:
UPDATE MyTable SET MyCol = REPLACE(MyCol , '[address]', '[some_other_field]')
You wanna rename names of columns in select part?
Use currentNAme AS NewName, or currentName NewName, for each name you wanna rename.
Depends on the DBMS weather one or other or both syntax are supported (sql-server -> go with AS). Test with 1 column name to see which one works then use that one till end
select
id AS myNameForId,
name AS myNmeForName,
.......
......
,upddate
from bank AS a
join bankreg AS b
on
a.id = b.id
where (condition)
OR
select
id myNameForId,
name myNmeForName,
.......
......
,upddate
from bank a
join bankreg b
on
a.id = b.id
where (condition)

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

SQL update query issue

I just accidentally noticed that a Query like
Update tableA tableA set id = '5'
Works fine. Should this give error as I am using table name twice here.
Any thoughts why is this working fine ?
Because the second tableA is seen as an alias. There is no constraint in regards to the name of the alias, so you can have the same name on the alias as the table name.
your code exactly equal
Update tableA as 'tableA' set id = "5"
or
Update tableA as "tableA" set id = "5"
this is simple alias as Sql Alias Tutorial

How do I Update a column that is like %another column% in SQL

I would like to run an Update on a table based upon values in another table.
So I have Table 1 with Column A
And I have Table 2 with Column B
I want to run an update so that every row in Column A gets updated with 'RANDOM STRING' if Column A is LIKE Column B.
Pretty simple up to here. However the string in Column B could occur anywhere in the String in Column A.
So the query should run something like this
UPDATE Table1
SET ColumnA = 'RANDOM STRING'
WHERE ColumnA LIKE '%Table2.ColumnB%'
However no rows get updated when I use this, though the WHERE condition should definitely return results
I am running SQL server 2008
try this
update table1
set ColumnA = 'RANDOM STRING'
where ColumnA in
(select table1.ColumnA from table1 inner join table2 on table1.ColumnA like '%'+Table2.ColumnB+'%')
In MySql,
You can do this.
UPDATE Table1
SET ColumnA = 'RANDOM STRING' from table1 , table2
WHERE table1.ColumnA LIKE concat('%',table2.columnb,'%')

Writing a single UPDATE statement that prevents duplicates

I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234