Alter column data type in Hive - hive

we need to alter the table column data type from string to date. While am trying to do am getting the below error. Could you please help.
hive> describe sales_staging;
OK
cust_id string prod_num string
qty int sale_date string
sale_id string
Time taken: 0.151 seconds,
Fetched: 5 row(s)
hive> alter table sales_staging CHANGE sale_date sale_date DATE ;
FAILED: Execution Error, return code 1 from
org.apache.hadoop.hive.ql.exec.DDLTask. Unable to alter table. The
following columns have types incompatible with the existing columns in
their respective positions :sale_date
hive>

You can't give same name to column you wish to change datatype of. use like this
ALTER TABLE sales_staging CHANGE sale_date sale_date_new DATE;
refer this Apache Hive Wiki

you can't change the existing string type data to date type. but we can able to solve this issues in 2 ways.
create another table with the same columns count but the data type is date where the column you need string to date, then use insert command to export old table data to new table by casting the string to date.
add a new column to existing table with datatype as date. overwrite the table itself by casting the string to date into the new column.
ex:
I have orders table
describe orders;
order_id int
order_date string
order_customer_id int
order_status string
created another table ordersnew
describe ordersnew;
id int
odate date
cid int
ostatus string
now exported the orders data to ordersnew table
insert into ordersnew select order_id,cast(from_unixtime(unix_timestamp(substring(order_date,1,19), 'yyyy-MM-dd HH:mm:ss')) as timestamp) as strdate, order_customer_id,order_status from orders;
substring(order_date,1,19), 'yyyy-MM-dd HH:mm:ss' this is the place you have to check and alter your query as per your data.
please check here for date conversions

Do the following steps:
step-1) check all the dates in field "sale_date" are valid dates or not. If yes then go to step-2
step-2) Check the date format, for DATE datatype format should be 'yyyy-MM-dd'. If the date format is 'yyyy-MM-dd HH:mm:ss' then you should try the following syntax:
alter table sales_staging CHANGE sale_date sale_date TIMESTAMP;

Related

How to convert a string column into a date column

Imported a date column from a CSV file where there are string values are shown as
date
44705
44704
44703
I want to convert the entire column into a date format as
date
"2022-05-22"
"2022-05-21"
"2022-05-20"
I have used this script which allowed me to generate the result.
SELECT dateadd(d,44703,'1899-12-30') as date
The question is, how could I apply this script for a range of values (there are 700 rows in the table). e.g 44000 to 44705. I would like all string values to be converted as a date format.
select cast (44705-1 as smalldatetime) gives 2022-05-25 00:00
So you could just update the column using the above.
See https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=dc24abb3025e0f3796e7d978ba406be3
New fiddle with update statement, this line will update all rows, as per test.
update #test
set pdate = cast(dateadd(d,tdate-2,'1899-12-30') as smalldatetime)
To convert a string to date we can add temporary date column and fill it and delete old column and rename new one to old one
alter table TableName add NewColumnName date null; --create temporary column
update TableName set NewColumnName =dateadd(d,cast(cast([date] as float) as int)-2,'1899-12-30') --fill it
alter table TableName drop column [date]--delete old column
EXEC sp_RENAME 'TableName.NewColumnName' , 'date', 'COLUMN'--rename new one to old one

How to convert two types of dates format into one date format in SQL?

I have three table in the database two of them contain dates however the dates are in two format, first 20-02-2011 and second is 25/09/2018. Let say each table have 10000 records and mixed with these two types of dates format. This why I why I create the column like --- (Transaction_Date, Varchar(10) Not Null)
I tried convert (Varchar(10),Transaction_Date,105)
and also tried replace(convert(varchar(10),Transaction_Date,105),'/','-')
However date and year functions are still not working.
Please suggest a possible way.
How about this?
select replace(date, replace(Transaction_Date, '/', '-'), 105)
That is: (1) convert to a date and (2) replace the slash before converting.
You need to remember about your culture. Saved format vs server culture. But this is very possible
select Cast('2-22-2011' as datetime) f1,
Cast('2/22/2011' as datetime) f2
I other words just use Cast
select cast(Transaction_Date as datetime) . . .
But you should as soon as possible get rid of columns that saves date as string and create new date/time column, and insert your date values there
alter table tbl add column temp datetime
update tbl set temp = Cast(Transaction_Date as datetime)
alter table tbl drop column Transaction_Date
alter table tbl add column Transaction_Date datetime
update tbl set Transaction_Date = temp
alter table tbl drop column temp

SQL - How to format date in column

The dates provided in the Expiration Date column appear as just numbers, ie 09192019, I would like the entire column to be in a date format ie; 09/19/2019
Code:
SELECT Expiration_Date
FROM Insurance
datatype of the table column is probably not date. I suggest you to fix the datatype by setting it to date by altering your table.
for this, use the below steps:
alter table Insurance add column (expiration_date_new date);
update Insurance set expiration_date_new = TO_DATE(Expiration_Date, 'MM/DD/YYYY');
alter table Insurance drop column expiration_Date;
alter table Insurance rename column expiration_date_new to expiration_date;
as a workarround you can convert Expiration_date column to date by
SELECT TO_DATE(Expiration_Date, 'MM/DD/YYYY') FROM Insurance

Changing the data type "varchar'' of a column to "DATE" in SQL/ORACLE

I have a table in a database created in oracle 10G. It contains a column of type 'VARCHAR' and stores date as string in this format-> 'dd-mon-yyyy' eg: '12-aug-2008'. Now I want to change the datatype of this column from VARCHAR to DATE. but when i perfrom this query->
ALTER TABLE sales_order
MODIFY COLUMN delivery_date DATE;
I get following error
ORA-00905: missing keyword
I have also tried :
ALTER TABLE sales_order
ALTER COLUMN delivery_date DATE;
I got the error :
ORA-01735: invalid ALTER TABLE option
However when i try to add a fresh column with DATE datatype it works fine.
example :
ALTER TABLE sales_order
ADD delivery DATE;
So, can anybody suggest me a way to change the datatype without deleting the column and its data.
It's the first one, with a slight modification:
ALTER TABLE sales_order MODIFY (delivery_date DATE);
But I'm not sure that will work for those particular datatypes and it also may not work depending on the current data.
You may find it necessary in that case to:
create a new column X of date type.
populate X based on the old column (may need several passes of data fix-ups to work).
delete old column.
rename X to old column name.
Although its a pretty old question, I'll put my solution here for people seeking for a solution:
Here's my solution and it works perfectly.
ALTER TABLE `sales_order` CHANGE `delivery_date` `delivery_date` DATE;
Thank you
modify a column then syntax is:-
alter table table_name modify column_name datatype;
but when you modify the column datatype column must be empty
Thanks for the hints! This got it for me.
alter table Table_Name
Alter column Column_Name datatype
GO
I too was needing to change from a VARCHAR to a date. I am working in SQL 2008 R2. I have found that if I bring in dates as a char or varchar and then change the type to date or datetime, I can catch time/date problems more easily.
THIS STATEMENT WILL FAIL IF YOUR DATA HAS ANY BAD DATES. (I break the date down into sections to find the bad date so I can correct and then I can alter the column type.)
Alternatively, you could create a new column, in which the data type is DATE. then pass the data in your varchar as a date .then drop your initial column and finally rename your new column to what it was initially...code below.
ALTER TABLE my_table ADD (new_col DATE);
UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
ALTER TABLE my_table DROP (old_col);
ALTER TABLE my_table RENAME COLUMN new_col TO old_col;
alter table employee add (DOB varchar(10));
if you add a column with datatype varchar and if you want to modify the datatype of DOB then you can use this command ->
alter table employee modify(DOB date);
Now the table is modified.

Change column type from character varying to timestamp without time zone

In Rails I created a string column called open_time, but then I realized I should use the datetime type. I did:
change_column :polls, :open_time, :datetime
But it said:
PG::Error: ERROR: column "open_time" cannot be cast to type timestamp without time zone
: ALTER TABLE "polls" ALTER COLUMN "open_time" TYPE timestamp
If I just drop the string column and add new datetime column, I will lose the data stored in the string column. Alternatively, in PostgreSQL I can add a column:
ALTER TABLE polls ADD COLUMN published_time timestamp;
Ant then try to get the data from the string column like:
UPDATE polls SET published_time = strToTimeStamp(open_time);
Are there any functions I can use as strToTimeStamp to convert character varying type to timestamp without time zone?
.. are there any functions I can use as strToTimeStamp that can convert
character varying type to timestamp without time zone type?
Use to_timestamp() to convert string data to type timestamp and alter the data type of the column in place:
ALTER TABLE tbl ALTER COLUMN col TYPE timestamp
USING to_timestamp(col, '<your pattern here>');
See:
Alter character field to date
Cast varchar type to date
Don't think you can do that, since it's limited by capability of database. The idea is doing following:
Created a new column with a different name
Copy over the column contents after data convert for each row
Drop the original col
Rename the new col to to the original col name
These steps can be placed into same migration file and keep them into a transaction for in case.