CSV date format is DD/MM/YYYY like this 16/11/2016. All the date become 0000-00-00 in MySQL. How to solve this differences?
As of now I can think of two solutions to you problem:
Create a additional VARCHAR field to insert those values (like 16/11/2016), and create a TRIGGER on INSERT to update the date field by converting the string date to 'YYYY-mm-dd' type.
These links may help
https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
http://www.w3resource.com/mysql/date-and-time-functions/mysql-str_to_date-function.php
In this step also, Create a additional VARCHAR field to insert those values (like 16/11/2016) and after import is done run a UPDATE query to update your date field using SET dateField = convertDate(dateFromSheet)
Related
I have a table in which there is a column called "DATE" which contains dates in the format "23-Nov-2017" as datatype VARCHAR. I'm trying to convert this VARCHAR column and store it in a new column called "NEWDATE" of datatype DATE.
I have created the new column "NEWDATE" of type DATE and I am trying to use the STR_TO_DATE() function to perform the conversion. However, I can't get it to work for some reason.
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%m-%Y');
The NEWDATE column is not updated with any values after the statement. I guess this means that the statement does not execute. What am I doing wrong?
EDIT: I have also tried STR_TO_DATE(DATE,'%d-%b-%Y'). However there is still no change to the values in the NEWDATE column
Your format '%d-%m-%Y' does not match your actual date string "23-Nov-2017"
The %m is for numeric month and you have an abbreviated text month
Use %b for 3 char month values like this:
STR_TO_DATE(DATE,'%d-%b-%Y')
EDIT: WorkBench issue
That is just a Workbench config setting to stop you accidentally issuing a HUGE update. You can either turn that setting OFF or frig it a bit by giving it a WHERE clause that will allow it to run like below. Below assumes this table has an id column
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id<10000000;
Or
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id>0;
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
Im using LOAD DATA INFILE to import a csv file, the files date format is 29/11/2010 and the database format is 2010-11-29, what can i use to format the date inside the query?
I've tried str_to_date:
SET date_start = STR_TO_DATE(#from_date,'%Y-%m-%d'),
but that only inserts 0000-00-00
MySQL 4.x
LOAD DATA will try to insert your dates as they are. It isn't aware about format and in common case you can not apply some post-processing to your fields (well, different from some format which is allowed inside LOAD DATA syntax itself) - and you can not adjust your values via SET keyword like in MySQL 5.x
Instead you can do following steps:
Declare your table's column as VARCHAR. Let it name be record_date
Do your LOAD DATA query. It will load your dates into record_date column
Add new column to your table, let it be temp_date - with type DATE: ALTER TABLE t ADD temp_date DATE
Update your temp_date column: UPDATE t SET temp_date = STR_TO_DATE(record_date, '%d/%m/%Y')
Drop your VARCHAR date column: ALTER TABLE t DROP record_date
Finally, rename column with correct DATE type to original one: ALTER TABLE t CHANGE temp_date record_date DATE
As result, you'll have your dates loaded into your table as DATE date type. Replace record_date to the name which your original column has.
MySQL 5.x
You can use SET keyword and natively replace procedure, described above. So just do:
LOAD DATA INFILE 'file.csv'
INTO TABLE t
(#date)
SET record_date=STR_TO_DATE(#date, '%d/%m/%Y')
-sample above is for one column and you'll need to add others (if they exist). Date column name is also record_date - so change it to actual name too.
Try something like
update tablename SET date_start = date_format(#from_date,'%Y-%m-%d')
Use DATE_FORMAT() . It will Formats the date value according to the format string. Mysql
date_format(#from_date,'%Y-%m-%d')
I have been researching this for a week and still no luck. I have a txt file that loads into 'temploadsi' table via php. The text file will always have old data that is already in the database so I only want the "new" data. My idea was to have a loading table 'temploadsi' and then compare the time stamps and import only "new" data into table 'tempdatasi'. The issue is that the time stamp column is formatted like 'MM/DD/YYYY HH:MM:SS' and that will not go into the mySQL datetime field. Therefore I have it set as a text field. I would like to convert the text into a datetime field like 'YYYY-MM-DD HH:MM:SS'. I have looked at STR_TO_DATE and DateTime functions but unable to update 'temploadsi'. Can you all help out?
The reason why you failing to update the field is because the time of your data is 24:59:59 which is not valid. The maximum time i think is 23:59:59, so in order to convert that to date, you need to use the following format
SELECT DATE_FORMAT(STR_TO_DATE(datetime,'%m/%d/%Y %x:%x:%x'), '%Y-%m-%d 23:59:59')
FROM table1
See SQLFiddle Live Demo
So if you wnt to update your field, you can use this query
UPDATE tableName
SET your_columnname = DATE_FORMAT(STR_TO_DATE(your_columnname, '%m/%d/%Y %x:%x:%x'), '%Y-%m-%d 23:59:59')
and this query assumes that your column you are updating has VARCHAR datatype.
Something like Str_to_date('12/10/2012 23:59:59','%m/%d/%y %h:%i:%s')
Don't get why you couldn't find this though
[Mysql Help]
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
I have a few new tables where I am now doing a bulk insert from a .txt file
there is about 5,000 rows of information. The problem I have is that, I have
no idea how to convert a column with varchar to a datetime... I am somewhat new to
SQL, so it's a new challenge. there is 7 columns with that I can do a bulk insert
to varchar but not datetime due to format.
12/06/89, 03/06/07,05/06/68 and so on
I would like to make this a DateTime. If anyone can offer a solution I would
be very thankful.
I am using sql 2008 r2 web
For eg.
select convert(datetime,'3/13/41',1)
Something like this:
SELECT CAST('12/06/89' AS DATETIME)
from: http://msdn.microsoft.com/en-us/library/ms189491.aspx
-- Set date format to day/month/year.
SET DATEFORMAT dmy;
GO
DECLARE #datevar datetime2 = '31/12/2008 09:01:01.1234567';
SELECT #datevar;
GO