Convert value in all rows to VARBINARY(50) - sql

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

Related

Update redshift column value with modified data from other column

I have a redshift table which is used for tracking, and as a result its pretty huge. I need to update one column after applying some text operations and extracting a value from another column.
The query that I have managed to write works only for one row.
UPDATE schema.table_name SET data_id = (SELECT split_part(regexp_substr(data_column,'pattern=[^&]*'),'=',2)::BIGINT FROM schema.table_name where id = 1620) WHERE id = 1620;
How do I get it to work for every row in the table.
UPDATE
schema.table_name
SET
data_id = SPLIT_PART(REGEXP_SUBSTR(data_column, 'pattern=[^&]*'),'=',2)::BIGINT;
Just don't put WHERE id = 1620; at end of update query.
Updates are not efficient in Redshift. If you have a huge table and you intend to update every single row, you should instead copy the data (with the updated column) to a new table and then flip them.

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?

CONCAT in Oracle

I've been researching the concat function extensively but hit a wall creating a temp table. I have two columns: ID (ex. 4323) and Source (ex. PHI). I want to add a column that includes a prefix of "API-" to the ID column (ex. API-4332). Anyone have any insight?
All rows?
UPDATE TheTable
SET id = 'API-'||id;
Or,
UPDATE TheTable
SET id = CONCAT('API-',id);
EDIT:
Your problem statement led me to believe you wanted to create a new column ("I want to add a column..."). Sorry for the confusion. I have changed the answer to update the ID 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.

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