Alter column in BigQuery correcting typo - sql

I have a table in BigQuery with a typo:
id,typo_column
1,Unknown
2,Unknown
3,Uknown
I want to replace Uknown by Unknown. Is there an easy way to do this in BigQuery?

You would normally do this as:
UPDATE yourproject.yourdatabase.yourtablewithtypo
SET typo_column = 'Unknown'
WHERE typo_column = 'Uknown';
There is no need to update every row in the table. There is no need to use regular expressions, and there is no need to run logic for replacement on rows that don't have the incorrect value.

You can use DML to fix all entries with typos:
UPDATE yourproject.yourdatabase.yourtablewithtypo
SET typo_column = REGEXP_REPLACE(typo_column, r"(Uknown)", "Unknown")
WHERE TRUE
See the docs for a more complete example.

Related

Can I Search for a Specific Row and Update it?? Google BigQuery Question

I am new to BigQuery and was wondering if there was a way to search for a specific row, then individually update its columns using a Query. I am very new to database and SQL, and would love some of your help.
Yes you can. Typically this is achieved through data manipulation language (DML). For this specific example you would want use an UPDATE statement.
Documentation on this can be found here:
https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#update_statement
As a word of caution verify your where clause before you execute to ensure you are targeting the correct rows.
For example:
UPDATE your_table
SET assigned_to_user = 'updated_value' -- the value you want to update to
WHERE -- this is where you define your criteria to narrow down to a subset of rows
criteria_column = 'target_values' -- this is your critera
-- and criteria_column_2 = 'other_value' -- if you need to expand more critiera just use logic operators like `and` or `or`
You can also test out what will be updated by issuing a select statement with the same criteria as your update and seeing which columns come back, for example:
select *
from your_table
WHERE
criteria_column = 'target_values'

Oracle Update table to set specific attribute value in semicolon separated values

I have a column where I have values like:
Email_Password+oi8hu907b;New_eMail+Y;Email_Username+iugbhijhb8
Now I want to update New_eMail attribute for all rows which has Y to N without affecting anything else.
Please advise.
i hate it but...
update table
set column = replace(column,'New_eMail+Y','New_eMail+N')
where column like '%New_eMail+Y%'
you don't need the WHERE clause but if you put a functional index on the table it may be quicker with it
Since it may be the only place in the string where '+Y;' occurs the following statement may do the trick:
update <your_table>
set <your_column> = replace(<your_column>,'+Y;','+N;')
where instr(<your_column>,'+Y;')>0
This solution differs from the others provided because it does not depend on the value of the email address.
My answer is a slight improvement over the answer from user davegreen100
Since they don't allow me to post it as a comment, I add it here.
update <<tablename>>
set <<columnname>> = replace(<<columnname>>,';New_eMail+Y;',';New_eMail+N;')
where <<columnname>> like '%;New_eMail+Y;%'

My SQL Update Statement updates even if the value is the same

I want to update a column value. But my Update procedure statement updates even the value of the this column is the same.
UPDATE TableName
SET ColumName=#ParameterName
WHERE Id=#ParameterId
Any Idea?
Thank you.
From what I get from your post, you are wondering why MySQL is updating even when the value is not changed. Well, you don't check beforehand if the value is really different, so that's just what an update statement with an ID does...
You can add additional condition AND (COALESCE(ColumName,'')<>COALESCE(#ParameterName,'')
so you're only updating when those are different.
UPDATE TableName
SET ColumName=#ParameterName
WHERE (Id=#ParameterId) AND (COALESCE(ColumName,'')<>COALESCE(#ParameterName,''))
The coalsece in my example assumes that ColumnName is of type varchar if is a numeric value use AND (COALESCE(ColumName,0)<>COALESCE(#ParameterName,0) instead.

REPLACE query format in DB2

I have a set of json format stored in a column and now i need to replace a particular word. How to use the replace query. Every time i use it, i'm getting token exception. please advise. Its a DB2 i'm using
I have 3 columns
Name Age Data
ABD 15 [{"Name":"ABC","type":"Regular","Math":18}]
In the Data column, I need to do a replace for "type", It should be StudentType.
REPLACE(Data,'type','StudentType');
This did not work. How to do it?
Thanks much in advance
Just like #mustaccio pointed out, if you use REPLACE in select statement it will just return your data with 'StudentType' instead of 'type'. This does not actually change data in your database. If you want to update your data you need UPDATE statement
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'OldString','NewString')

Update A multi-valued field in Access

I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.