Want to delete specific column in a table - sql

I want to delete specific values/data from one column with the WHERE condition. Putting in another way, I don't want to delete the complete row. Is it possible?

You can re-set values using update:
update t
set col = NULL
where . . .;

Yes it is possible to delete the particular value/data from the column using WHERE condition. You can use the below code in SQL command to delete that particular column.
Update Table_Name set Column_Name=null where Column_Id=xxx
or you can even update that by changing the Column_name value to an empty string ' ' Like below
Update Table_Name set Column_Name='' where Column_Id=xxx

Related

Deleting entire data from a particular column in oracle-sql

Recently I have started learning Oracle-sql. I know that with the help of DELETE command we can delete a particular row(s). So, Is it possible to delete entire data from a particular column in a table using only DELETE command. (I know that using UPDATE command by setting null values to entire column we can achieve the functionality of DELETE).
DELETE
The DELETE statement removes entire rows of data from a specified
table or view
If you want to "remove" data from particular column update it:
UPDATE table_name
SET your_column_name = NULL;
or if column is NOT NULL
UPDATE table_name
SET your_column_name = <value_indicating_removed_data>;
You can also remove entire column using DDL:
ALTER TABLE table_name DROP COLUMN column_name;
In SQL, delete deletes rows not columns.
You have three options in Oracle:
Set all the values to NULL using update.
Remove the column from the table.
Set the column to unused.
The last two use alter table:
alter table t drop column col;
alter table t set unused (col);
Use Invisible Type, which is from an oracle 12cR2.
ALTER TABLE LOG1
MODIFY operation INVISIBLE
It is a better than drop of a particular column.If you need to visible you can get back by altering with an VISIBLE of a column name.
update employee set commission=nvl2(commission,'','')
this will remove all the data from the column

Delete a specific field from a database

How can I delete the value of a field from a database, using query code? I only know where the field is located in the database. (ex: column['phone number']row[3])
It should be something like this "DELETE FROM ... WHERE ..."
There is nothing like delete the field in database i.e. if you want to remove the complete row then you can do that easily like this
DELETE FROM TABLE WHERE COLUMN1=#myValue
But if you wanted to remove one value of the row in that case you should update that value as NULL
UPDATE TABLE SET COLUMN1=NULL WHERE COLUMN1=#myValue
I have used where clause from my imagination. you can always use whatever you want.
The DELETE statement allows you to delete RECORDS.
When you want to set a field to an empty value, use the UPDATE statement.
See this for more info.
UPDATE [YOUR_TABLE] SET [YOUR_FIELD] = NULL WHERE [YOUR_PRIMARY_KEY] = VALUE
Yes first you need to use delete command. Then after this insert update command then only the records will be deleted from the database. I agree that you have deleted records but its not showing because you haven't updated it.

Convert value in all rows to VARBINARY(50)

I want to upgrade all columns in a table, what I am aiming to do is retreive a column from the row it will update then update it, something like:
update works
set encrpyted_item_no = (CAST(RTrim((
select unencrypted_item_no
from works
where name = name
) AS VARBINARY(50))
I know that query is wrong, it's just an example to show you what I am aiming to do.
I want it to select the column unencrypted_item_no from its row then update that same row with the data it gets from unencrypted_item_no, doing this for the whole table.
How would I accomplish this?
You shouldn't need to do a sub-select, referencing the other column in the set will work on a row by row basis, ie:
UPDATE works
SET encrpyted_item_no = CAST(RTrim(unencrypted_item_no) AS varbinary(50))
Shouldn't this be enough ?
update works
set encrypted_item_no = CAST(RTrim(unencrypted_item_no) AS VARBINARY(50))

Delete one column on the criteria of another SQL Server 2008

One attribute in a table became corrupted after a certain point in a table of mine. I want to delete every pat_coun attribute if it has an ID that begins with 11 (number, not text). So I don't want to get rid of any of the records in the database, just clear out the attribute pat_coun if it's ID begins with 11
DELETE pat_coun from myTable
WHERE id %11
Just want to make sure this is right before I go deleting stuff. Thanks.
To clear out an attribute, do NOT use the DELETE function! That deletes a row from your table!
You need to use UPDATE instead:
UPDATE myTable
SET pat_coun = NULL
WHERE id LIKE '11%'
If you want to delete a record (a row) you can use
DELETE FROM myTable
WHERE condition
If you just want to "clear" a particular column you should use
UPDATE myTable
SET pat_coun = 0 // or NULL or whatever you please
WHERE condition
For condition IMHO you should convert your number to string and check like this
WHERE CONVERT(VARCHAR(20), pat_coun) LIKE '11%'
try this
update myTable
set pat_coun = null
where id like '11%'

SQL Update or Replace function?

I have a table which contains multiple columns.
Column 1 Column 2 Column 3
unique identifier alphanumerical value numerical value
The unique identifier is currently using the values from Column 2. If I wanted to use the values from Column 3 instead, which would be better suited for my situation? Replace or Update? Or is there another way I should go about doing this.
I'm using TOAD for Oracle for what it is worth.
Thank you.
Turns out what the OP wanted to do was simply set column1 to the value in column3, no replace was necessary. Just a straight update, as in:
UPDATE TheTable SET column1 = column3;
IF
you want to make a change in the table THEN
use UPDATE
ELSE IF
you want to just view the column1 values mapped with column3 values for particular instance THEN
use INSERT
The otherway around use UPDATE to change the contents of table, whereas replace is a function which really doesnt makes any changes in the contents of the table but just shows u the changed output.
You would use both.
update sometable set column1 = replace(column1,column2,column3)
You might want to do the following first to make sure you're replacing what you want to replace:
select replace(column1,column2,column3) from sometable