I added a column using following commands (I used quotation marks so that N is in upper case in my column name)
ALTER TABLE new_table
ADD “Name” VARCHAR(50);
However, I see "nam" column in my table now after running that command.
How can I drop that column?
ALTER TABLE new_table
DROP COLUMN "Name";
I get following error:
ERROR: column "name" of relation "new_table" does not exist
The following statement causes this error:
ALTER TABLE new_table
DROP COLUMN "Name";
seems you are not using double quote " in first query bus some others quotes try suing the same chars “”
ALTER TABLE new_table DROP COLUMN “Name”;
Check how Postgres is storing the column name when you are using a double quote in column name:
select table_name, column_name from information_schema.columns where
table_name='tab1';
Also, you may view the same when you execute a select statement:
select * from table;
Copy the same column name text in your alter statement. Here is the sample column name with both kind of double quotes in PostgreSQL:
create table tab1(data varchar(30));
alter table tab1 ADD “Name1” varchar(50);
alter table tab1 ADD "Name2" varchar(50);
select *from tab1;
select table_name, column_name from information_schema.columns where table_name='tab1';
alter table tab1 drop column “Name1”;
alter table tab1 drop column "Name2";
table_name column_name
tab1 data
tab1 “name1”
tab1 Name2
Here is the link to the fiddle
Note: Avoid using a double quote in the table name, column, etc. In case you use you have to ensure that the same names are specified in all queries.
Edit:
It's simple. If you use "Name" (not same as “Name”, notice quote angle) in the column name then you have to refer the column as "Name". In case “Name” is used then you have to refer by “Name”. The quotes need to match.
Another observation is when "Name" used as column name it makes the column name as Name (N uppercase) as opposed to all lowercase column names by default in the database but needs to be referred as "Name".
Related
After removing a column from a table by:
ALTER TABLE MyTable
DROP COLUMN IF EXISTS MyColumn
In BigQuery UI I Can see that the column was deleted successfully & I can't query the specific column but when I query DDL I can see that the column still exists in the scheme:
SELECT DDL FROM MyDataSet.INFORMATION_SCHEMA.TABLES
WHERE DDL LIKE '%MyTable%'
What am I doing wrong?
This is a nasty, undocumented side effect of Bigquery's Time Travel. Time Travel makes it unsafe to use ALTER TABLE statements in bigquery.
Demonstration of problem:
create table apu.time_travel_problem
( id int64
, name string
);
select column_name, data_type
FROM apu.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'time_travel_problem';
column_name
data_type
id
INT64
name
STRING
This is all normal so far, but after an ALTER TABLE everything goes odd:
alter table apu.time_travel_problem drop column name;
select column_name, data_type
FROM apu.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'time_travel_problem';
column_name
data_type
id
INT64
name
STRING
The column we just dropped is still there!
Now try this:
alter table apu.time_travel_problem add column name string;
Column `name` was recently deleted in the table `time_travel_problem`. Deleted column name is reserved for up to the time travel duration, use a different column name instead.
Solution:
Do not use ALTER TABLE in bigquery. Instead DROP and reCREATE using a temporary table.
This is a jinja template which I use:
/* {{TABLE}} */
CREATE TABLE IF NOT EXISTS {{DATASET}}.{{TABLE}}_migration
OPTIONS (expiration_timestamp = timestamp_add(CURRENT_TIMESTAMP(), INTERVAL 8 HOUR))
AS SELECT * FROM {{DATASET}}.{{TABLE}};
DROP TABLE {{DATASET}}.{{TABLE}};
CREATE TABLE {{DATASET}}.{{TABLE}}
(
{{COLUMN_DDL}}
);
INSERT INTO {{DATASET}}.{{TABLE}}
(
{{COLUMN_LIST}}
)
SELECT
{{COLUMN_LIST}}
FROM {{DATASET}}.{{TABLE}}_migration;
I'm looking for some help to create a new column based on values from another column - if this is even possible... This is not an ideal solution but I'm out running out of options.
I need to replace the beginning folder paths, change the direction of the \ and change the extension
Existing Field:
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
New Field:
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
Oracle version Version 19.2.1.247
Thank you in advance
You can add a new column to your table named NewField:
Alter table TableName add NewField varchar(500);
Then update NewField by replacing some characters as you wish from ExistingField.
update TableName set NewField= replace(replace(existingfield,'\','/'),'.cca','.wav')
Here I have just replace '' with '/' and '.cca' with '.wav'.
To replace path also:
update TableName set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
DB-Fiddle:
Schema and insert statements:
create table mytable (existingfield varchar(500));
insert into mytable values('
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca');
Add new column:
Alter table mytable add NewField varchar(500);
Update query:
update mytable set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
Select query:
select * from mytable;
Output:
EXISTINGFIELD
NEWFIELD
\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
db<>fiddle here
How can one delete a column name [database].[dbo].[my_table].[col_name] in SQL server, that is a column name with dots and squared brackets. In other words, it is the column name that I wanted but prefixed by the database name and shema.
I tried many combinations based on internet e.g. here but didn't succeed.
Thank you.
I don't understand if you want to rename this column or drop it, but here is how to do both
CREATE TABLE JustTest(
Col1 INT,
[[database]].[dbo]].[my_table]].[col_name]]] INT
);
-- To rename the column use this
EXEC sp_rename 'JustTest.[[database]].[dbo]].[my_table]].[col_name]]]',
'NewName',
'COLUMN';
-- If the table is TempTable use this
EXEC tempdb.sys.sp_rename N'#TMP.[[database]].[dbo]].[my_table]].[col_name]]]',
N'NewName',
N'COLUMN';
-- To drop it use this
ALTER TABLE JustTest DROP COLUMN [[database]].[dbo]].[my_table]].[col_name]]];
In a delimited identifier ']' is escaped as ']]', '[' and '.' don't need to be escaped.
So like this:
create table #tt(id int, [[database]].[dbo]].[my_table]].[col_name]]] int)
alter table #tt drop column [[database]].[dbo]].[my_table]].[col_name]]]
I want to change the column comments on an existing hive table using hive 0.13. This works:
create table test (mycolumn int);
alter table test change mycolumn mycolumn int comment 'hello';
But I can't find a way to do this without repeating the name of the column and the type, both of which are irrelevant to the change. For example:
alter table test change mycolumn comment 'hello'; leads to an error.
If this was for one column it would not be a big deal but I want to do this for large numbers of columns in tables that were not commented. I know this could be done with a script that simply copies the column name and its type but would be nice to know if there were something simpler. Thanks
You can do this using ALTER command.
CREATE TABLE my_table
(id INT COMMENT 'id comment',
name STRING comment 'name comment');
-- change column comment as below.
ALTER TABLE my_table CHANGE id id INT COMMENT 'another comment';
-- see changed column
DESC EXTENDED my_table;
ALTER TABLE test_change CHANGE a1 a1 INT COMMENT 'this is column a1';
Directly its not supported to edit column property for comment the other way around is
alter table dev.travel change num2 clm_num1 int comment 'a new column added';
Now I want to change above one lets do ;
alter table dev.tkt change clm_num1 num2 int comment 'a new column added';
alter table dev.tkt change num2 clm_num1 int comment 'a new column added with new comment';
I need to change every double precision field of a table to numeric(15,3) type,
how can i do this job quickly with a stored procedure that iterate through the field of a given table and if the type is double precision alter column to numeric ?
following query should return query to update these tables... you can add table filter if you want. Copy the result and run it.
select 'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + 'TYPE numeric(15,3)'
from information_schema.columns
where data_type = 'double precision'
and table_name = 'YOUR_TABLE_NAME'
TEST IT BEFORE RUN IT. I DID NOT TEST THIS YET.
While another question will do it for all columns; a simple "alter table [tablename] alter column [columnToAlter] type numeric(15,3). You shouldn't need to run them through a cursor; any value that's not going to be affected by this should remain unaffected.
If you can't do it by changing the datatype itself, a simple update [tablename] set [columnname] = cast(columnname as numeric(15,3) should also work.
Hope that helps!