Date time error while creating table in mysql - mysql

I am getting an date/time error while creating table in my sql ? how can i fix this?
create table ibi.stpl_toll_day_fare_fy_ex as
select
cast(date_time as date) as calendar_date,
I get this error
Error code :1292.incorrect datatime value.

Please check if the value in the date_time field of the existing table has the right data (if may have non relevant value to date)

From clause missing. You can query as below:
Using temporary table for demo, you can use your table
create temporary table yourtable(date_time date);
insert into yourtable(date_time) values (now());
create table stpl_toll_day_fare_fy_ex as
select
cast(date_time as date) as calendar_date from yourtable;
Select * from stpl_toll_day_fare_fy_ex;

Related

trying to ALTER a table based on the results from a SELECT statement in mySQL

when i use the following select statement i can return the date differences between date_due and date_returned.
select Date_Due,Date_Returned,
datediff(Date_Due,Date_Returned) as Day_Diff
From tp_rental
However i cant get these values to replace the current null values in the tp_rental table. I tried the below statement also thinking it would create a new column.
alter table tp_rental
ADD DayDiff as DATEDIFF (Date_Due,Date_Returned);
my errors say there is a problem with the syntax near as DATEDIFF
any ideas? thanks in advance
Generated column expressions need to be enclosed in () (see the manual). You also need to add a column type definition:
alter table tp_rental
ADD DayDiff INT as (DATEDIFF(Date_Due,Date_Returned));

Hive dynamic partition by unix timestamp

I am creating a table in Hive, running a mapper transformation and then saving a table. I want to partition the table based on when I ran the Hive query.
I create the table:
CREATE EXTERNAL TABLE IF NOT EXISTS testtable (
test_test STRING
) PARTITIONED BY (time STRING)
LOCATION 'loc/table'
;
Then run the transformation and save the table while trying this:
FROM (
MAP
one.test_test
USING
'python job.py'
AS test1
FROM
one
) test_step
INSERT OVERWRITE TABLE testtable PARTITION (time=unix_timestamp())
SELECT CAST ( test_step.test1 AS STRING ) AS test_test
;
However, when I do the
time=unix_timestamp()
, I get an exception. How would I go about doing this?
Thanks
I think it should work if you use dynamic partitioning (https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-DynamicPartitionInserts). The partition field is just another column in the table, so if you have a value for the column in your query, then Hive will automatically put it in the right partition. So your statement would look something like this
FROM (
MAP
one.test_test
USING
'python job.py'
AS test1
FROM
one
) test_step
INSERT OVERWRITE TABLE testtable PARTITION (time)
SELECT CAST ( test_step.test1 AS STRING ) AS test_test,
unix_timestamp() as time
;
Doing it like this might create a lot of partitions though, as the value of unix_timestamp() will change during execution of the query. It would be better to use an extra statement to create the partition first and then insert.
EDIT: To add a partition beforehand you'd need to set the timestamp you want somehow, e.g. a parameter for the script. Then
ALTER TABLE testtable ADD PARTITION (time=your_timestamp_here);
This would go before your original query where you replace unix_timestamp() with your_timestamp_here (which of course would be a valid unix timestamp).

Unable to copy data from one table to a new table in MySQL due to a String - Date type clash

I've a date column in table1 filled with character data that looks as follows:
InDate:
2022015
2012015
2122014
2112014
2102014
2092014
I've converted that data to following format:
InDate:
02-02-2015
02-01-2015
02-12-2014
02-11-2014
02-10-2014
02-09-2014
by using the following command:
update table1
set InDate = DATE_FORMAT(str_to_date(InDate, '%d-%m-%Y'), '%d-%m-%Y');
But obviously this only changed the look of the string data in the InDate column. Now, this still didn't suffice my situation and I needed the column itself to be in Date type.
So, I created a new empty table called table2 with the same structure as table1 (except for InDate column, which I now declared as Date type) and tried copying the old data (which is having InDate in my required format (02-02-2015)).
INSERT INTO table2
SELECT *
FROM table1;
But I'm getting the following error message:
Error Code: 1292. Incorrect date value: '02-02-2015' for column 'InDate' at row 1
I need this column to be in Date type with the data intact. This table has millions of records that I need to work with. I really need to get around this error.
Please help!
This code block maybe help use :
Not update table1.
insert into table2
(indate)
select date_format(str_to_date(indate
,'%d-%m-%Y')
,'%d-%m-%Y')
from table1;
Please try this.
INSERT INTO table2(`InDate`)
SELECT CONCAT('\'',InDate,'\'')
FROM table1;
Hope this helps.

curdate() causes error in trigger

i am using navicat8 for MySQL for writing trigger, bellow is my trigger statement.
insert into two(name,curdate())
select name from one
but it will display error while saving trigger.
insert into two(name, date_col)
select name, curdate()
from one
First you have to name the columns you want to insert into, and then the values. If you want to fill all columns from your table you can leave the column name part and do
insert into two
select name, curdate()
from one
You should specify the name of the column instead of passing CURDATE().
Use the following syntax,
insert into two (name, colName)
select name, CURDATE()
from one
UPDATE 1
So here's what you are going to do.
You need to add UNIQUE constraint on column Name on table two.
To alter the table,
ALTER TABLE tableNamehere ADD CONSTRAINT two_uq UNIQUE(name);
SQLFiddle Demo Link

mysql changing values in table?

I have to change the values in my table and create an temporary table from that. But not with UPDATE, since I have to keep the original table.
for example a table like Table(id, date), I have to create a temp table by changing the date values. The ones which are NULL must be CURRENT_DATE().
How can I manage that??
Create temporary table with (id,date)
then
INSERT INTO tempoaryTable (id,date)
SELECT id, IFNULL(date,CURRENT_DATE())
FROM yourTable
You can use INSERT-SELECT form to copy all values from original table to temporary, setting corresponding field to CURRENT_DATE().
Syntax might be off as I don't now mysql
insert into temp_table
select id,coalesce(date,current_date) from mytable