How to migrate column data to new data type - oracle

I need to migrate table's column to another data type with some modifications. Given: table 'USER' with column 'ORDER_TIME' (DateTime format). It's required to change column type to NUMBER, convert that time to a minutes (e.g. 8:00 AM = 480 mins.) and to store new value. As i understand, i need to create new column with required NUMBER data type, iterate over all records, do some recalculations, store new value, drop old column and rename new one to an actual name. So, basically, algorithm is:
ALTER TABLE USER ADD ORDER_TIME_MINS NUMBER(4) NULL;
iteration and recalculation
ALTER TABLE USER
DROP COLUMN ORDER_TIME
RENAME COLUMN ORDER_TIME_MINS TO ORDER_TIME;
Can you suggest me some reading on how that iteration may looks like?

You can try like this:
update tablename
set columnName = to_number(to_char(to_date(yourDateColumn,'hh24:mi:ss'),'sssss'))/60
And if your time is in format 08:00 PM then like
update tablename
set columnName =TO_NUMBER(TO_CHAR(TO_DATE('08:00 AM','HH:MI AM'),'SSSSS'))/60
Rest of the steps which you have shown ie, dropping and renamin are fine.

Related

How to replace NULL values in one column to 0 (of a very large table) without creating a new column of the desired results added to the table in HIVE?

I am trying to replace all of the NULL values to 0 in a column of a big table in HIVE.
However, every time I try to implement some code I end up generating a new column to the table. The column I am trying to change/modify still exists and still has the NULL values but the new column that is automatically generated (i.e. _c1) is what I want the column I am trying to modify, to look like.
I tried to run a COALESCE but that also ended up generating a new column. I also tried to implement a CASE WHEN, but the same results ensued.
Select *,
CASE WHEN columnname IS NULL THEN 0
ELSE columnname
END
from tablename;
Also tried
SELECT coalesce(columnname, CAST(0 AS BIGINT)) FROM tablename
I would just like to update the table with the other columns being as is but the column I want to modify still has its original name but instead of NULL values it has 0's that replaced them.
I don't want to generate a new column but modify an existing one.
How should I do that?
Use insert overwrite .. option.
insert overwrite table tablename
select c1,c2,...,coalesce(columnname,0) as columnname
from tablename
Note that you have to specify all the other column names required in select.

How alter column type with using toUInt32OrZero function in clickhouse?

I have String column in clickhouse table.
I try alter table with modify type to UInt32:
ALTER TABLE main.abonents
MODIFY COLUMN
device_type UInt32 DEFAULT 0
but have error:
Received exception from server:
Code: 6. DB::Exception: Received from 5.200.55.122:9000. DB::Exception: Cannot parse string 'mo' as UInt32: syntax error at begin of string. Note: there are toUInt32OrZero function, which returns zero instead of throwing exception..
It's clear, clickhouse use toUint32 function on string like 'mobile' and throw exception. And its advice to use function toUInt32OrZero to convert type.
How can i use toUInt32OrZero function with ALTER TABLE??
There's no such way (as far as I know).
You can achieve it with a second table. Let's create one:
CREATE TABLE main.abonents_new AS main.abonents;
Then we have to alter column in that new table.
This table has no data yet, so it won't raise exceptions:
ALTER TABLE main.abonents_new MODIFY COLUMN device_type UInt32 DEFAULT 0;
Then, make sure no new data is written to main.abonents. We'd like to keep everything in place when we'll transfer the data to the new table.
Insert the data using INSERT INTO SELECT query. Make sure to list all the fields with the same order; wrap device_type to the converter function (toUInt32OrZero) :
INSERT INTO main.abonents_new SELECT field1, field2, ..., toUInt32OrZero(device_type) AS device_type, ..., fieldN FROM main.abonents;
Then, make sure that everything's alright (that rows count is the same, device_type was converted as intended, etc), then rename the tables:
RENAME TABLE main.abonents TO main.abonents_old;
RENAME TABLE main.abonents_new TO main.abonents;
(or, you may DROP the older table instead; although I'd keep the old data to be able to restore if things'd go south)

In hive, is there a way to specify between which columns to add a new column to?

I can do
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT)
to add a new column to the end of my non-partition columns and before my partition columns.
Is there any way to add a new column to anywhere among my non-partition columns?
For example, I would like to put this new column user_id as the first column of my table
Yes it is possible to change the location of columns but only after adding it in the table using CHANGE COLUMN
In your case, first add the column user_id to the table with below command:
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT);
Now to make user_id column as the first column in your table use change column with FIRST clause:
ALTER TABLE table_name CHANGE COLUMN user_id user_id BIGINT first;
This will move the user_id column to the first position.
Similarly you can use After instead of first if you want to move the specified column after any other column. Like say, I want to move dob column after user_id column. Then my command would be:
ALTER TABLE table_name CHANGE COLUMN dob dob date AFTER user_id;
Please note that this commands changes metadata only. If you are moving columns, the data must already match the new schema or you must change it to match by some other means.
Ah, here's the explanation for why you listed user_id twice (it's not a type):
// Next change column a1's name to a2, its data type to string, and put it after column b.
ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;
// The new table's structure is: b int, a2 string, c int.
No, it is not possible.
One solution is to create new table using "CREATE TABLE AS SELECT" approach and drop older one.

Add a column, with a default value, to an existing table in oracle

I created a table named- books and have a column in that by the title 'color' . Initially I have null values in the column 'color'. Now, when I run the following query :
alter table books modify color default 'blue';
schema is formed but on doing select *from books , all the values in column color are still null. What would be the correct query to fire?
here is the link:
http://sqlfiddle.com/#!4/f4210/1
Of course. Alter table just changes the table structure but not the content. New entries will get the default.
To update the existing values run a sql-update query like:
update books set color='blue' where colore is null;
If you now inserting into table then only will come with default values. This statement don't know about previous contents of this table. In non technical language, you are telling oracle to do so now on-wards. This statement will not perform check to old values.
alter is ok for the next values to be inserted: try to insert lines without specifying a value for column color, value should be blue.
But this does not work for existing values, for which you just need an update:
update books set color = 'blue';
Hi this query will be used to add column with default value in existing table in oracle.
alter table <table_name> add <column_name> <contraint> default <default_value> not null;
example:
alter table books add record_status number(1,0) default 1 not null;
alter table books add color varchar(20) default 'blue' not null;

SQL Table Column that uses input from Another Column in Same Table

I want to have a column in table which calculates the value depending on the other column in the same table.
For example,
I have "Validity" Table with Columns "DateManufactured", "DateExpires"
The Date Expires column must calculate value suppose adding 30 days for Datemanufactured.
How Can we do this in Visual Studio2010->DataSet Design-> DataTable Column-> Properties->Expression
See relevant Image here
What could be the possible expression for this in terms of SQL Server Expressions?
Please Suggest optimal Solution.
Thanks in advance.
I believe you are looking for DateAdd
SELECT DATEADD(day, 30, '15 Dec 1988')
select dateadd(day,30,getdate())
this will take the current date(getdate())
add 30 days to it.
I would suggest creating a stored procedure to insert your data into your table with parameters of the values that needs to be inserted. you can then do your calculation based on your date parameter. Example:
Create Procedure InsertValidity
(
#City varchar(20),
#Area varchar(20),
...
#DatePosted datetime,
...
#UserID
)
as
declare #DurationFrom datetime
set #DurationFrom = (select DATEADD(dd,30,#DatePosted)
insert into Validity (City, Area, ..., DatePosted, ... UserID)
values(#City, #Area, ..., #DatePosted,...#DurationFrom,...#UserID)
This should solve your problem. Just complete the script by replacing the ... with your other data then execute the stored procedure in your application.

Resources