"Update" statement has "unrecognized name" problem on "Set" - google-bigquery

When I try to update an existing table by inserting a new column, BigQuery states the error "Unrecognized name: start_geo_concat". This is odd because I already created a column in another table using this same query formula. I tried changing the name or casting as a STRING but that did also not work. Any ideas?
UPDATE
`red-freedom-357915.CyclisticBikeShare.BikeGeoData`
SET
start_geo_concat = CONCAT(slat_round, " , ", slng_round)
WHERE
ride_id IS NOT NULL

You say "inserting a new column". Does that mean that you're trying to create a new column using an UPDATE statement? If so, you can't do that. The column first has to exist in order to UPDATE it.
Try doing this first:
ALTER TABLE `red-freedom-357915.CyclisticBikeShare.BikeGeoData`
ADD COLUMN start_geo_concat STRING;

Related

SQLite - add a column if it does not exist

I am new to SQLite. I want to add a column if it does not exist.
How to check if the column name exists and then add if it does not?
I tried
ALTER TABLE table ADD COLUMN colname INTEGER ON CONFLICT IGNORE
But it shows an error
Result: near "ON": syntax error
Any advice how it can be achieved?
Thanks in advance.
First get a list of table column names - as list - with something like:
select group_concat(c.name) from pragma_table_info('table_name') c;
Then do a CASE expression on whether the new column name you want to add exists in the list above. More info at: https://www.sqlite.org/lang_expr.html

Google BigQuery: Add new column to a table with constant value

I would like to know a way of adding one additional column to a BigQuery table, that will populate all the rows for this newly created column with specific constant value.
I know how to create column with NULL values:
ALTER TABLE project_id.dataset.table
ADD COLUMN A STRING
But my goal is to also add the ingestion time with CURRENT_TIMESTAMP() function. Is it even possible with one command? Or maybe I need to apply subsequently some second command?
Seems like a solution is to use another query after the mentioned one:
UPDATE project_id.dataset.table SET A = CAST(CURRENT_TIMESTAMP() AS STRING) WHERE 1=1

Is it possible to run a query to rename the fieldnames of each field?

I have like 20 datasets, each with 28 fields without the first row having fieldnames. So MS Access assigned 'Field1', 'Field2'.... 'Field28' as fieldnames for each column. I'd like to know if it is possible to run a query to rename them? I couldn't find any information online and I tried manually entering the names but it is taking too long.
Access SQL does not support changing the name of an existing field.
You could create a SELECT query and alias the field name:
SELECT Field1 AS FieldA FROM YourTable;
Then you would use the query instead of the table whenever you want to display the aliased name.
Or you could use a "make table" query to create a new table with the new field name:
SELECT Field1 AS FieldA INTO NewTable FROM YourTable;
Or you could execute an ALTER TABLE statement to add the new field, then an UPDATE to populate the new field with the old field data, and finally DROP the old field. But that seems like too much work.
Instead of SQL, consider using VBA to rename the field:
CurrentDb.TableDefs("YourTable").Fields("Field1").Name = "FieldA"

How to reverse data of a column into another column in MySql?

I need to run a script to update a field of a table in mysql by reversing the string stored in another field of the same table. Original column is term and the one to be filled with the reversed is revTerm. I use this, but it produce an error. Any suggestions?
UPDATE `tu_cla_terms` WHERE tId = '11583' SET revTerm = REVERSE(term)
You have the syntax of an UPDATE query wrong. SET comes before WHERE:
UPDATE `tu_cla_terms` SET revTerm = REVERSE(term) WHERE tId = '11583'
If tId is a numeric column you don't want quotes around its value either.

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'