SQL Server Query Command - sql

I have an SQL database table, I want to find everything in a table where the 'Room' says 'DISPOSED', Insert 'DISPOSED' into the 'Status' field and then delete the entry in 'Room'.
Basically moving the entry from one field to another (if the 'Room' field has 'DISPOSED' in it)
Hope this makes sense.
Thanks for any help.

Update table_name SET room='', Stauts='DISPOSED' where Room='DISPOSED'
OR
Update table_name SET room=null, Stauts='DISPOSED' where Room='DISPOSED'

The SQL is
UPDATE table
SET status = room, room = null
WHERE room = 'DISPOSED'

Related

how to delete a value from a column in SQL

How can I delete only the field of a column without deleting the entire row? I have a table that owns the fields:
ID, Name, Mail, Phone, Company and Would like to delete only the email of a person without deleting all the fields.
If I use:
DELETE FROM TEST_TABLE WHERE MAIL = 'myemail#gmail.com'
that way will delete everything and I want to delete just the email
you can use this
Update myTable set MyColumn = NULL where Field = Condition.
References
1- How do I set a column value to NULL in SQL Server Management Studio?
2- UPDATE, DELETE, and INSERT Statements in SQL
try
UPDATE
TEST_TABLE
SET
MAIL = ''
WHERE
id = your_id
or
if you want delete the field
ALTER TABLE TEST_TABLE
DROP COLUMN MAIL;
It is good practice to update the field with a NULL instead of leaving it blank, this indicates that there is a missing value and will later allow you to do queries where something is NOT NULL, which will give better results if it isn't returning bad data. Remember, garbage in, garbage out.
UPDATE TEST_TABLE SET MAIL = NULL WHERE MAIL = 'myemail#gmail.com'

Teradata - How to find when was table last UPDATED not Altered

I want to know how to find when was a Teradata table was last UPDATED. Everywhere I look I am getting when was it last Altered. Is updated the same as Altered?
I got the code for Alter table :
SELECT TABLENAME, LASTALTERTIMESTAMP
FROM DBC.TABLES
WHERE DATABASENAME = 'Schema'
AND TABLENAME = 'table'
ORDER BY LASTALTERTIMESTAMP DESC
What is it for UPDATE table? Thanks
Good day!
To find the time the table last was updated, you can try to parse query log table.
For example, let your table name is TNAME and it is located in schema SNAME.
So you can use the following query:
select
username,
firststeptime,
querytext
from pdcrinfo.dbqlobjtbl_hst
where
statementtype = 'Update'
and querytext like 'insert into SNAME.TNAME%'
qualify row_number() over (partition by 1 order by firststeptime desc) = 1
In terms of SQL requests, UPDATE and ALTER are two different things. ALTER modifies the table structure (DDL) -- i.e. adding / modifying a column. UPDATE modifies the table data (DML) -- i.e. adding / deleting rows.
As far as I remember, there's no easy way to see when a table was last modified (updated) unless you add some custom auditing fields (i.e. lastAlterTimestamp) that you set each time you write to the table.
IF you want to know when a table was updated, you might think about using a trigger.

SQL update, check changed value

I have some problem with the update in Sybase DB.
I need, for audit, get a field that value is changed.
I do an update for all fields because I don't know what the user change.
Suppose we have this table with one row. Name,Surname,id
My update is:
update table
set Name = #name, Surname = #Surname, id = #id;
Now I need now only the fields that are changed in the update.
Sorry my English and thanks

Updating columns values from another table SQL

I want to update my table from another table in another database.I have two table that has two same columns.There are ID and iexp column.What i want is update every row from k_monster table to my database's k_monster table but there are other columns such as iHP iMP so i want to just update iExp column.what do you suggest?
Assuming Target_Database is the database where the table is that you want to update and Source_Database is the database where the table is you are using to update from.
Your query should look something like this.....
USE [Target_Database]
GO
UPDATE t
SET t.iexp = S.iexp
FROM K_monster t
INNER JOIN [Source_Database].[Schema].[K_monster] S
ON t.ID = S.ID
GO
Please check this link similar to this type of question:
Additionally I would suggest you to search before you ask any questions.
UPDATE record in one database with values from another in SQL Server 2008?
This link has similar answer to your question.
More links:
Update database table from one SQL Server database table to another?
https://dba.stackexchange.com/questions/30228/how-to-update-one-database-from-another
https://dba.stackexchange.com/questions/58371/sql-update-column-with-data-from-another-table
With Regards

SQL update set table if value in table A is equals to value in table B

this query is working fine.
UPDATE data
SET unit_id='a3110a89'
WHERE unit_id='7d18289f';
Now, I need to run this query over 30 times
so I made csv file import it to the DB with this command:
COPY mytable FROM 'D:/test.csv' WITH CSV HEADER DELIMITER AS ','
Now I have table called my table with 2 columns OLD and NEW
i want to search the table "data" in column unit_id anywhere there if the value equals to the value in table "mytable.old" replace it with the value "mytable.new" on the same row.
I tried to run this query but I get an error:
UPDATE data
SET unit_id=(SELECT mytable."old" FROM public.mytable)
WHERE unit_id=(SELECT mytable."new" FROM public.mytable)
error:
more than one row returned by a subquery used as an expression
I think i'm just trying to do it in the wrong way...
thx for the help!
by the way Im using PostgreSQL
Your subqueries need to be correlated to the outer update:
UPDATE data
SET unit_id = (SELECT mytable."new" FROM public.mytable where data.old = mytable.old)
WHERE unit_id in (SELECT mytable."old" FROM public.mytable);
That is, set the unit_id to the "new" value, when you find the "old" value in the table.
Can you try like this,
UPDATE data A
SET A.unit_id=B.old
FROM (SELECT mytable."old",mytable."new" FROM public.mytable) B
WHERE A.unit_id=B.new
UPDATE data A
SET unit_id = B."old"
FROM public.mytable B
WHERE A.unit_id = B."new"
;
BTW: it looks like you also have old and new swapped in your question. Do you really want A's value to be set to B's old field?