Date Time Conversion in MySQL - mysql

I imported a CSV file into MySQL. The CSV file contains Date/Time in the format 'mm/dd/yyyy hh:mm'.
It gets imported into MySQL only as text. So I did import it as text. I then created another field hoping to convert this text value to date time in this format 'mm-dd-yyyy hh:mm' OR in this format 'YYYY-mm-dd hh:mm'. But it does not work. Below is my code.
ALTER TABLE table1 ADD COLUMN StartDateNEW DATETIME;
SET SQL_SAFE_UPDATES = 0;
UPDATE table1 SET StartDateNEW = STR_TO_DATE(StartDate, '%m/%e/Y %H:%i');
SET SQL_SAFE_UPDATES = 1;
Sample Data:
Some more sample data:
I have been trying this for over an hour now. Can someone please help?

If you are loading data using the LOAD DATA INFILE syntax, it should be possible to handle conversion on the fly.
Assuming that your source csv file looks like:
StartDate, EndDate, Value
"1/10/2012 10:05", "1/11/2012 11:51", abc
"1/13/2012 08:00", "1/15/2012 09:01", abc
You can defined columns StartDate and EndDate as datetime datatype, and simply do:
LOAD DATA INFILE '/path/to/my/file.csv' INTO TABLE mytable
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' -- or '\n'
IGNORE 1 LINES
(#StartDate, #EndDate, Value)
SET
StartDate = STR_TO_DATE(#StartDate, '%c/%e/%Y %k:%i'),
EndDate = STR_TO_DATE(#EndDate, '%c/%e/%Y %k:%i')
;
NB: and if you are curently not using LOAD DATA INFILE... I would still recommend migrating your code to use it. This is the proper way to do this in MySQL, it works on all clients.... and it is very fast (see this link).

Based on your sample data, the correct format string for STR_TO_DATE is
%c/%e/%Y %k:%i
%c allows for single digit month numbers
%e allows for single digit day numbers
%Y four digit year
%k allows for single digit hours
%i two digit minutes
All the format strings are described in the manual for DATE_FORMAT.
Demo on dbfiddle

A quick hack is as follows:
Open CSV File in Excel
Select the date column
Change date format to yyyy-mm-dd using cell format popup
Save file
Import to MySQL
This was you don't need to import in text form and then try to convert the data into DateTime.

Related

Extract Date from SQL field

Does anyone know a function I can use to extract the date after the last ',' or last '|' in a field?
Values are in the below format:
ON24_NEW_LEADS_29032020
Newsletter|20210803
Many thanks,
JD

Convert String Field to Date value in a different format with an SQL select query

I have a table with a column start_date. Unfortunately, the datatype of the column is VARCHAR. Values in this column are either null or a string date in any format.
I have a requirement to extract the data from this table to a CSV file using BCP command. But while extracting, start_date value is expected to be in YYYYMMDD format in the output file.
The problem here is for some records, start_Date will be in DD/MM/YYYY format and for some others it may be in another format. For example : YYYY-MM-DD.
Is there any way so that I can convert the start_date value to the format YYYYMMDD irrespective of the actual format using the select query itself.
select convert(DATE,'13/12/2019',112) as 'AliasName'
This function will help u in fetching the data in the format of YYYYMMDD irrespective of how the format of ur input column is..This function uses 3 arguments 1.THE DATA TYPE WHICH U WANT TO CONVERT 2. YOUR INPUT COLUMN WHICH U WANT TO CONVERT 3.STYLE (style number is: Each format has its own style number for example if you give style number as 103 it will convert to dd/mm/yyyy or if 112 it will convert to YYYYMMDD ..there are many style You will find it if u search about convert function) . Hope this will help!
Hope this helps!!
select
*,
(CASE WHEN [Date] like '%/%' then TRY_CONVERT(datetime,[Date],103)
else [Date] END) as [New_DATE]
from TableName;

Convert string yyyymmddhhmmss into hh:mm:ss format in SQL

With the string DATETIME yyyymmddhhmmss like 20160125173013, I would like to convert this string into hh:mm:ss (17:30:13) as a new column called "Time" in a table with sql update statement. However I am only able to convert it into 17:30 using the stuff function.
Is there any possible solution to convert?
In my statement
UPDATE db
SET Time =convert(time, stuff(substring(DATETIME,9,6),3,2,':'))
FROM db
WHERE Time IS NULL
Real Output=17:13:00.0000000
But my expected output is 17:13:00
Thanks a lot!
Here is the miracle for mysql:
SELECT time(str_to_date('20160125173013', '%Y%m%d%H%i%s'));
Do you have an actual DATETIME field? If so you can use DATE_FORMAT():
UPDATE mytable SET my_time=DATE_FORMAT(my_date, '%H:%i:%s')
If you don't have a native DATETIME field I hope you can convert it to one, as irregular, quirky formats cause trouble and introduce a lot of overhead when parsing to convert. STR_TO_DATE() can do the opposite of DATE_FORMAT() and convert from arbitrary strings to native DATE or DATETIME values.
Don't confuse STORAGE with PRESENTATION
Declare #YourTable table (DateTime varchar(25),Time time)
Insert Into #YourTable values
('20160125173013',null)
Update #YourTable
Set Time = stuff(left(right(DateTime,6),4),3,0,':')
Select *
,FormatedTime = Format(cast(Time as datetime),'HH:mm')
From #YourTable
Returns
DateTime Time FormatedTime
20160125173013 17:30:00.0000000 17:30
Based on the use of the STUFF function I believe this is Microsoft SQL Server, and not MySql.
Therefor, you can do something like this:
UPDATE db
SET [Time] = CAST(SUBSTRING([DATETIME], 9, 2) +':'+
SUBSTRING([DATETIME], 11, 2) +':'+
RIGHT([DATETIME], 2) As Time)
WHERE [Time] IS NULL
Your string is no format, SQL Server will cast implicitly
DECLARE #YourDateTimeString VARCHAR(100)='20160125173013';
The following query will cut the first 8 digits and cast them to DATE, which works implicitly (unseparated format). The time is cut from the right side, then the two :-signs are stuffed into the right places:
SELECT CAST(LEFT(#YourDateTimeString,8) AS DATE)
,CAST(STUFF(STUFF(RIGHT(#YourDateTimeString,6),5,0,':'),3,0,':') AS TIME);
The result
2016-01-25 17:30:13.0000000
If you need this as string without the trailing .0000000 (which is a pure output format question and should be done in your presentation layer!) you can just use LEFT(). The input of this function is string (again implicitly casted), the output is a text which looks like a time.
SELECT CAST(LEFT(#YourDateTimeString,8) AS DATE)
,LEFT(CAST(STUFF(STUFF(RIGHT(#YourDateTimeString,6),5,0,':'),3,0,':') AS TIME),8);
The result
2016-01-25 17:30:13
If you ar eusing SQL server then use Convert function
Declare #VarCharDate varchar(max)
Declare #VarCharDate1 varchar(max)
--Declare
set #VarCharDate = '20160125173013' --- YYYYMMDDHHMMSS
--Convert
set #VarCharDate1 =(select SUBSTRING(#VarCharDate,0,5) + '/' +
SUBSTRING(#VarCharDate,5,2) + '/' + SUBSTRING(#VarCharDate,7,2)
+ ' ' + SUBSTRING(#VarCharDate,9,2)
+':'+SUBSTRING(#VarCharDate,11,2) +':' + RIGHT(#VarCharDate,2))
select #VarCharDate1
select Convert(varchar(8),convert(datetime, #VarCharDate1, 120),114)

mysql data loading error (date order)

I have a large .txt file including 20 millions of lines of strings like:
"CS1221|123.10|17.02.2012 09:10:23,5676"
The first is customer id, then separated by "|" we have $ amount of transactions and finally date and time (dd.mm.yyyy hh:mm:ss,ssss).
I am trying to load it to Mysql table but it isn't accepting this ordering as TIMESTAMP (it accepts YYYY-MM-DD hh:mm:ss,ssss)
Is there any piece of code written in mysql that helps me?
You can use the STR_TO_DATE method to convert that date format. Try something like this:
SELECT STR_TO_DATE('17.02.2012 09:10:23,5676', '%d.%m.%Y %H:%i:%s,%f');
Should yield:
2012-02-17 09:10:23.567600
So your INSERT query would look something like:
INSERT INTO your_table (all, relevant, field_names) VALUES ("CS1221", "123.10", STR_TO_DATE('17.02.2012 09:10:23,5676', '%d.%m.%Y %H:%i:%s,%f'));

Date imported from csv into mysql as 0000-00-00

I have some data saved as txt file. I am saving the txt file as csv in order to import it into a database using my sql workbench.
what I am doing is the following:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv' INTO TABLE mytable FIELDS TERMINATED BY ',' ENCLOSED BY '"' lines terminated by '\n';
But one of my column is a date, and it is imported as 0000-00-00
How to import it in a good way ?
Edit
Here is what my csv contains:
id task hoursWorked begindate enddate
0 task1 15 11/17/2012 11/18/2012
1 task2 20 11/18/2012 11/20/2012
2 task3 20 12/4/2012 12/5/2013
3 task4 22 1/5/2013 1/7/2013
Please have a try with this one:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv'
INTO TABLE mytable
LINES TERMINATED BY '\n'
(id, task, hoursWorked, #var1, #var2)
SET begindate = STR_TO_DATE(#var1, '%m/%d/%Y'),
enddate = STR_TO_DATE(#var2, '%m/%d/%Y');
For more info see LOAD DATA and STR_TO_DATE
Note: I deleted the FIELDS TERMINATED BY ',' ENCLOSED BY '"' part, cause I neither see , nor " in your CSV. But if it works fine for you the way it is, feel free to revert :)
The default date format is YYYY-MM-DD:
mysql> SELECT ##date_format;
+---------------+
| ##date_format |
+---------------+
| %Y-%m-%d |
+---------------+
... thus MySQL won't recognise stuff like 11/17/2012 as a proper date. In theory, you should be able to change the default format, but I'm not sure it can be done in session scope and I wouldn't recommend to change it for the whole server. It's better to make the transformation yourself. The trick is to insert the value into a variable rather than a column.
Additionally, there're two other issues:
Your CSV file contains a header line.
Your fields are not separated by ,.
Assuming your file uses tabs as separators, the complete command would be:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv'
INTO TABLE mytable
FIELDS TERMINATED BY '\t' ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, task, hoursWorked, #tmp_begindate, #tmp_enddate)
SET begindate = STR_TO_DATE(#tmp_begindate, '%m/%d/%Y'),
enddate = STR_TO_DATE(#tmp_enddate, '%m/%d/%Y');
MySQL doesn't actually allow to change ##date_format anyway:
mysql> SET ##date_format='%d/%m/%Y';
ERROR 1238 (HY000): Variable 'date_format' is a read only variable
As the MySQL 5.6 manual explains:
This variable is unused. It is deprecated as of MySQL 5.6.7 and will
be removed in a future MySQL release.
Also, at Date and Time Types we can read:
MySQL retrieves values for a given date or time type in a standard
output format, but it attempts to interpret a variety of formats for
input values that you supply (for example, when you specify a value to
be assigned to or compared to a date or time type). For a description
of the permitted formats for date and time types, see Section 10.1.3,
“Date and Time Literals”. It is expected that you supply valid values.
Unpredictable results may occur if you use values in other formats.
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
If it didn't work, just add columns to your CVS for year, month and day and separate day, month and year of your date, and use the following:
set date_column = concat(#year , '-' , #month , '-' , #day)