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

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 ;

Related

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?

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

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....

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))

SQL SELECT or INSERT INTO query

I'm working with SQL Server 2000. I need to take the results from one column (VALIMIT) and insert them into another column (VALIMIT2012) in the same table (lending_limits).
My question is do I need to do a SELECT query first, or do I just start with an INSERT INTO query and what the proper syntax would be for the INSERT INTO query.
You can do this with an UPDATE statement:
update lending_limits
set VALIMIT2012 = VALIMIT
Neither. You don't insert columns, you insert rows, so what you want is an update:
update SomeTable
set VALIMIT2012 = VALIMIT
Note: It looks like you have one column per year, which is bad database design. If you have different data for each year, you should put that in a separate table, so that you get the year as data, not part of the column name.
UPDATE TableName SET VALIMIT2012 = VALIMIT

select the rows affected by an update

If I have a table with this fields:
int:id_account
int:session
string:password
Now for a login statement I run this sql UPDATE command:
UPDATE tbl_name
SET session = session + 1
WHERE id_account = 17 AND password = 'apple'
Then I check if a row was affected, and if one indeed was affected I know that the password was correct.
Next what I want to do is retrieve all the info of this affected row so I'll have the rest of the fields info.
I can use a simple SELECT statement but I'm sure I'm missing something here, there must be a neater way you gurus know, and going to tell me about (:
Besides it bothered me since the first login sql statement I ever written.
Is there any performance-wise way to combine a SELECT into an UPDATE if the UPDATE did update a row?
Or am I better leaving it simple with two statements? Atomicity isn't needed, so I might better stay away from table locks for example, no?
You should use the same WHERE statement for SELECT. It will return the modified rows, because your UPDATE did not change any columns used for lookup:
UPDATE tbl_name
SET session = session + 1
WHERE id_account = 17 AND password = 'apple';
SELECT *
FROM tbl_name
WHERE id_account = 17 AND password = 'apple';
An advice: never store passwords as plain text! Use a hash function, like this:
MD5('apple')
There is ROW_COUNT() (do read about details in the docs).
Following up by SQL is ok and simple (which is always good), but it might unnecessary stress the system.
This won't work for statements such as...
Update Table
Set Value = 'Something Else'
Where Value is Null
Select Value From Table
Where Value is Null
You would have changed the value with the update and would be unable to recover the affected records unless you stored them beforehand.
Select * Into #TempTable
From Table
Where Value is Null
Update Table
Set Value = 'Something Else'
Where Value is Null
Select Value, UniqueValue
From #TempTable TT
Join Table T
TT.UniqueValue = T.UniqueValue
If you're lucky, you may be able to join the temp table's records to a unique field within Table to verify the update. This is just one small example of why it is important to enumerate records.
You can get the effected rows by just using ##RowCount..
select top (Select ##RowCount) * from YourTable order by 1 desc