I have an INSERT query in ruby and I'm passing parameters from another table. One of the parameters is a timestamp value, for example: 2015-11-22 12:57:06 +0000 which is stored in a variable name created_at (of type Time)
insert into my_tbl set
name = '#{name}',
created_at = #{created_at}
and I'm always getting errors while trying to insert it.
I've tried to convert it to string, and to use str_to_date function, but the problem is that I have a timestamp value.
How can I insert the value to the table?
INSERT INTO my_tbl (name, created_at)
VALUES (name, created_at.to_s.split(' +').first)
Format the input in mysql from chat and from ruby program insert and format in this chat with the sale format
Update:
In mysql :
Select to_date('"+varchar+"','dd/mm/yyyy'......
In Ruby:
Varchar='01/12/2015"
Of course with format
Related
I have a table with date values stored as strings, like '2012-01-15'. Some of them are invalid, like '2012-04-31'. I would like to insert the valid dates into a DATE type column in another table, and default the day to 1 if it is too large for the month.
DAYNAME seems to be the only function in MySQL that will check whether a date is valid. However, it issues a warning for an invalid date (in addition to returning NULL), which upgrades to an error in an INSERT or UPDATE statement.
So I'd like to do something like
INSERT INTO date_tbl (date_value)
SELECT IF(DAYNAME(date_string) IS NOT NULL, date_string, CONCAT(LEFT(date_string, 8), '1')
FROM date_string_table;
This fails with Data truncation: Incorrect datetime value: '2010-04-31' even though I am not actually inserting invalid data.
The problem with using INSERT IGNORE is running the risk of actually inserting invalid data, which I would really like to avoid.
EDIT Oct 5:
This problem can be reproduced without creating the intermediate table simply as
CREATE TABLE date_tbl (
date_val DATETIME
);
INSERT INTO date_tbl (date_val)
SELECT IF(DAYNAME('2012-04-31') IS NOT NULL, '2012-04-31', NULL);
I would like the above INSERT to insert NULL for that invalid date, instead of failing.
You can compare days of the proper date and last day for that month with LAST_DAY and STR_TO_DATE.
So your query would be:
INSERT INTO date_tbl (date_val)
SELECT IF(DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d')) > DAY(LAST_DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d'))), NULL,'2012-02-30');
DB Fiddle
A workaround is to use INSERT IGNORE and then validate after the fact:
SELECT date_value
FROM date_tbl
WHERE DAYNAME(date_value) IS NULL;
Should return zero rows.
CREATE TABLE orders(
order_id INTEGER PRIMARY KEY,
customer_id INTEGER,
order_date DATE,
ship_date DATE
);
INSERT INTO orders
VALUES
(1, 1200, '2013-23-10', '2013-28-10');
Is there anything wrong with the above code?
You are using 'YYYY-dd-MM' format whereas you need to use 'YYYY-MM-dd', try the following:
INSERT INTO orders VALUES (1, 1200, '2013-10-23', '2013-10-28');
Date format is 'YYYY-MM-DD'.
23 and 28 are not valid values for MM month component.
The error returned by MySQL is expected behavior.
Nothing necessarily wrong with what you are doing, if you are want MySQL to return an error message.
If you are wanting the statement execution to be successful, and to add a row to orders table, supply valid date values for the DATE columns.
Either change the literal values to match the format expected by MySQL, or use STR_TO_DATE function to convert the string from a specified format.
... , '2013-10-23' , ...
or
... , STR_TO_DATE('2013-23-10','%Y-%d-%m') , ...
I want to insert into table some particular values. However, I can not add date to my table. Other values can be added easily. I used this query
INSERT INTO `student` (`bdate`) VALUES ('30.05.1992');
I need to add in this format. I tried to use DATE_FORMAT('30.05.1992','%d.%m.%y')
it also didn't help.
INSERT INTO `student` (`bdate`) VALUES (STR_TO_DATE('30.05.1992', '%d.%m.%Y'));
If your field type is date then you can insert data in yyyy-mm-dd format only, even at the time of fetching data you can convert it into your own format.
If you want to insert date in your own format then you can use varchar data type, even at the time of fetching data it will not be optimized and you can get slowness.
It should be
INSERT INTO `student` (`bdate`) VALUES (STR_TO_DATE('30.05.1992', '%d.%m.%Y'));
as year is 4 digit such that 1992,so it should be %Y
I have a set of code that I'm coverting from Postgres to work on MySQL (PHPmyAdmin) which already has a section of inserts in the following type of format:
INSERT INTO ORD (TOTAL, SHIPDATE, ORDID, ORDERDATE, CUSTID, COMMPLAN)
VALUES ('101.4', '08-JAN-87', '610', '07-JAN-87', '101', 'A');
Since MySQL doesn't like the date as '08-JAN-87' it just ends up displaying 0000-00-00 in the date columns.
From the searches I've done so far the only solutions given seem to be converting it during the select statement so it displays correctly. I would like to know a method of changing the data itself. (not just a select statement)
This is for a uni assignment and the lecturer could only advise manually changing all the insert statments to a format it will accept. This might work for this case but in the long run or for a larger data dump this won't be possible.
You need to modify the import so that the data string is converted for MySQL
INSERT INTO ORD (TOTAL, SHIPDATE, ORDID, ORDERDATE, CUSTID, COMMPLAN)
VALUES ('101.4', STR_TO_DATE('08-JAN-87', '%d-%b-%y'), '610', STR_TO_DATE('07-JAN-87', '%d-%b-%y'), '101', 'A');
This tells mysql that the string 08-JAN-87 is a date in the format DD-MMM-YY and to convert it to a valid date datatype.
I have tried to insert date and time string formatted into mysql timestamp field by using following two methods but both shows me 0000-00-00 00:00:00
INSERT INTO test VALUES ( UNIX_TIMESTAMP('2013-08-05 18:19:03') )
INSERT INTO test VALUES ( UNIX_TIMESTAMP(STR_TO_DATE('2013-08-05 18:19:03', '%Y-%m-%d %H:%i:%s')) )
I believe first one should work as I am expecting but not sure why isn't parsing date and time?
The problem is that your field is defined as TIMESTAMP but UNIX_TIMESTAMP returns an int. Use INSERT INTO test VALUES ('2013-08-05 18:19:03' ) instead.