select statement - update the value of one field with a fixed value - sql

I have a select statement that returns a few fields from a table.
I want to update only the results of that select, giving a fixed value in one field.
I though of that, but it doesn't work:
UPDATE
(SELECT * from table.... where...)
SET field1=1

You didn't need a SELECT, just use a WHERE clause directly with the UPDATE to do this only for the rows that statify the condition in the WHERE clause:
UPDATE t
SET field1 = 1
FROM table AS t
WHERE ...

If your are using t-sql
UPDATE
SET field = fixed value
from tablename
where filed....

Related

How to show specific value in a column - sql (redshift)

I have a situation where I want to show a value that corresponds to one field in a table but in all rows. What is better is to show you an example in a screenshot:
What I want is to have the value of 3807 in every row? How can I do that?
THank you in advance.
Try using MAX as an analytic function:
SELECT
site_id,
bt_max_speed,
MAX(bt_max_speed_on_site_coverage) OVER (PARTITION BY site_id) bt_max_speed_on_site_coverage
FROM yourTable
ORDER BY site_id;
If you simply want to update one column so it has same value for all rows you can just use UPDATE without any conditions
UPDATE <table_name> SET <column_name> = <value>
In your case it would look like this, but with replaced with actual name of your table
UPDATE <table_name> SET bt_max_speed_on_site_coverage = 3807

using RIGHT to get data from column and update to add result to new column

I have a trouble to get this right.
I get data with this query
Select RIGHT(RTRIM(Nimi), 3) Riik from TABLE
But when I use this in update, then it has more then one result to insert.
So when I use it like this :
update #temp_table
set veerg2 = (Select RIGHT(RTRIM(nimi), 3) nimi2
from #temp_table a
where a.nimi is not NULL )
Then I get error on that, but I need to get these three last chars into new column.
Need help on that.
Your syntax looks like SQL Server. The correct query is most likely:
update a
set veerg2 = RIGHT(RTRIM(nimi), 3)
from #temp_table a
where a.nimi is not NULL ;
The two changes are:
The table alias defined in the from clause is used in the update rather than the table name.
The select is removed from the set. It is unnecessary.
You can also write this without the from:
update #temp_table
set veerg2 = RIGHT(RTRIM(nimi), 3)
where nimi is not NULL ;

SQL Error [512] [21000]: Subquery returned more than 1 value

Why does it not work? (SQL Server)
UPDATE
someTable
SET
name='AB'
WHERE
id IN (
SELECT t.id
FROM someTable t
WHERE t.name='ABC'
)
this one doesn't work too
UPDATE
someTable
SET
name='AB'
WHERE
name='ABC'
Because you must have a broken UPDATE trigger on the table.
A common error in triggers is not taking into account that a statement can affect multiple or zero rows and thus that the INSERTED/DELETED tables don't always contain exactly one row.
Look in the trigger for constructs like
SET #ID = (select ID FROM INSERTED)

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

How do you use the results from a query to automatically update a table

Basically, I have a calculated field I am creating using a select query. I want to use the results of this calculation to update a column in an existing table. How do I do that in the same query? How do I use the results of a calculated field to automatically update my table?
Use the select as the value in an update statement.
UPDATE <table> SET <column> = (SELECT ...)