Delete the column for the particular value - sql

Using Sql Server 2005
Table1
ID Name Value
001 Rajesh 90
002 Suresh 100
003 Mahesh 200
004 Virat 400
...
I want to delete the value from the table1 for the particular id
Tried Query
Delete value from table1 where id = '001'
The above query is not working.
How to make a delete query for delete the particular column
Need Query Help

There are at least two errors with your statement:
The word table will give a syntax error because it is a reserved word. You need to specify the table name of the specific table you wish to delete from.
Also you cannot write DELETE value FROM. It's just DELETE FROM. And note that it deletes the entire row, not just a single value.
A correct delete statement would look like this:
DELETE FROM table1
WHERE id = '001'
However if you want to change a single value to NULL you should use an UPDATE statement.
UPDATE table1
SET value = NULL
WHERE id = '001'
Of course this assumes that the column is nullable. If not, you'll have to fix that first. See this question for details:
Altering a column to be nullable

I think you want to set the value to null
update Table1 set value=NULL where id='001'

Related

Database After Update Trigger From Inserted But Not Inserted? [SQL Server]

I'm trying to create a SQL Server trigger that when an update on table 1 happens in the val2 column, it takes that value and updates table2. I'm struggling to understand how to do this properly on something that sounds easy.
Image of tables
I believe my issue is trying to set a value that is not actually from inserted. (The only value getting updated is val2). I don't know how I can pair where to do the update other than grabbing the ID also on the updated row to compare to the place I want to update. Any help/advice on this would be greatly appreciated. Below is my current trigger that does not work.
CREATE TRIGGER Pull_ID on table1
AFTER UPDATE
AS
BEGIN
#id_ = id from inserted
#val2_ = val2 from inserted
UPDATE table2
SET val2 = #val2_
FROM
WHERE table2.id = #id_
END
You need to build your trigger so that it can handle multiple rows - always.
So you a proper set-based approach, not relying on the false assumption that you only get one row.
Try this:
CREATE TRIGGER Pull_ID on table1
AFTER UPDATE
AS
UPDATE table2
SET val2 = i.val2
FROM Inserted i
WHERE table2.id = i.id
The Inserted pseudo table contains all the rows that have been updated - their new values after the UPDATE. Join that table with your Table2 based on the primary key column (assuming that's the Id) and use a proper, set-based, multi-row capable single UPDATE statement - and you're done!

UPDATE and REPLACE part of a existing column value with new column value

I am trying to update a existing column value with new value in sql table.
For example, My table has below data
table1:
ID
RequestName
1
Victor-123
2
Hello-123
3
Victor-124
4
Victor-125
5
Hi-123
6
Victor-126
In the above table I want to update Request Name column value wherever we have Victor, I want to replace with Victor-ID. For example
for ID 1 we have RequestName column value is Victor-123. I want to update it with Victor-ID-123 using Sql. I know we can do it with update sql statement, but if we have lot of data how to achieve that or replace 'Victor' with 'Victor-ID'. Since we might have different values in Request Name column. I want to update only column value with Victor to Victor-ID in table
Any help, I appreciate it
Thank you
UPDATE [tablename] SET [RequestName] = REPLACE(RequestName,'Victor','Victor-ID')
Here are two examples:
Example 1:
update table1
set RequestName = REPLACE(RequestName, 'Victor', 'Victor-ID')
where RequestName like 'Victor-%'
Example 2 (this one will let you use this for any name by changing the where clause):
update table1
set RequestName = LEFT(RequestName, CHARINDEX('-', RequestName) - 1) + '-ID-' + RIGHT(RequestName, LEN(RequestName) - CHARINDEX('-', RequestName))
where RequestName like 'Victor-%'
I don't believe example 1 actually requires the where clause though, as it should only affect those with "Victor". However, if you have something like "Victor1-123" that should not be changed, the where clause will prevent that.
Edit: Just occurred to me that example 1 can be adjusted to:
update table1
set RequestName = REPLACE(RequestName, 'Victor-', 'Victor-ID')
This will allow you to eliminate the where clause.

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?

SQL insert row with one change

I have this table:
Table1:
id text
1 lala
And i want take first row and copy it, but the id 1 change to 2.
Can you help me with this problem?
A SQL table has no concept of "first" row. You can however select a row based on its characteristics. So, the following would work:
insert into Table1(id, text)
select 2, text
from Table1
where id = 1;
As another note, when creating the table, you can have the id column be auto-incremented. The syntax varies from database to database. If id were auto-incremented, then you could just do:
insert into Table1(text)
select text
from Table1
where id = 1;
And you would be confident that the new row would have a unique id.
Kate - Gordon's answer is technically correct. However, I would like to know more about why you want to do this.
If you're intent is to have the field increment with the insertion of each new row, manually setting the id column value isn't a great idea - it becomes very easy for there to be a conflict with two rows attempting to use the same id at the same time.
I would recommend using an IDENTITY field for this (MS SQL Server -- use an AUTO_INCREMENT field in MySQL). You could then do the insert as follows:
INSERT INTO Table1 (text)
SELECT text
FROM Table1
WHERE id = 1
SQL Server would automatically assign a new, unique value to the id field.

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%'