mysql data loading error (date order) - mysql

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'));

Related

Date and time conversion query in SQL

In my database table, there is a date column i.e. EXPECTED DATE which is in dd-mm-yyyy format, and the datatype of the column is text. Now, I want to convert the format to yyyy-mm-dd. But the date is not changing at all and also when I tried to get the timestamp for the expected date column . I am getting some errors. For date coming I have used this STR_TO_DATE. But the year is not coming like what I expect and the timestamp also.
For example:
select STR_TO_DATE ('30-11-2011', '%d,%m,%y') as date ;
returns a result as
2020-11-30
And for timestamp
select STR_TO_DATE ('2011,11,30 12,30,45', '%y,%m,%d, %H,%I,%S');
I am not getting errors.
Please help me get the correct answers for this problem.
For the first query you need to use the %Y. Remember that it is always better to use "Y" for the years when you are writing a query for year.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For the second one also, you can use '%Y' in the place of '%y'. For minutes, use '%i' not '%I'. For hours and minutes, you can use whatever you like.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");
Refer to the below documentation for more clarification on SQL commands.
You need %Y (capital Y) for the 4 digit year, when using MySQL's STR_TO_DATE. Also, minutes is represented by %i, not %I, the latter which is hours on a 0 to 12 scale. So use:
SELECT STR_TO_DATE('30-11-2011', '%d-%m-%Y');
SELECT STR_TO_DATE('2011,11,30 12,30,45', '%Y,%m,%d %H,%i,%S');
For the first query you need to use the %Y'.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For minutes, use this one only '%i'.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");

how can i find the data between the two different dates like (29-05-2021 to 08-06-2021) in MYSQL table

i was write a mysql query to get the data between the two dates(ex: 29-05-2021 to 08-06-2021). but it shows the only current month(june) data only. it didn't show the (may) month data.
example:
i was stored john at 30-05-2021, sam at 31-05-2021, raja at 07-06-2021, ramesh at 05-06-2021. When i was run the query only showed raja, ramesh. it's not show the john and sam.
this is my mysql query
SELECT staff_name FROM staff_info WHERE join_date <= '09-06-2021'
in database i was stored date in varchar method and format is d-m-Y.
can any one help to solve these problem.
You are going to have to convert the text which only you know is a date to a date type so MySQL knows how it can process it easily as a date
str_to_date() is used to convert text into a Date type
SELECT staff_name
FROM staff_info
WHERE str_to_date(join_date, '%m/%d/%Y') between '2021-05-31' AND '2021-06-06'

MySQL: update table values with DATE FORMAT %d-%m-%y TO %y-%m-%d

During a database import, I kinda messed things up. So now I have a date column that is of DATE type, but the year and day are in the wrong places: the day ended up appearing in the year column and MySQL has auto formatted it as year further worsening things.
I have values in a table like below:
dates(dddd-mm-yy)
2030-05-12
2021-06-13
2012-12-13
I want them like so:
dates(yyyy-mm-dd)
2012-05-30
2013-06-21
2013-12-12
Please note that I actually want to UPDATE the column, I have seen and tried many examples on SELECT.
Thanks in advance.
You can use substring() and concat() to concat whatever you want. Below is the statement for your reference(table is tb_test, the column name is col_date).
update tb_test set col_date = concat('20',substring(col_date,7,2),'-',substring(col_date,4,2),'-',substring(col_date,1,2));
I dont know if this can be classified a "solution", maybe more of a work around, but it worked flawless for me.
After reading this, i realised that my problem started when i tried to import my dates in the format dd-mm-yy, Hence Mysql assumed my day to be year and presented my dates as yydd-mm-yy
My dates were formatted as yydd-mm-yy and i have severally tried the query
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%Y-%m-%d');
This query ran but made no changes to the Table because in the eyes of Mysql it has helped me format my dates as yyyy-mm-dd right from when i imported my DB, but still my dates are in yydd-mm-dd
So i thought, what if i try a query like;
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%d-%m-%y');
This way Mysql switched the days and years then provided my dates as yyyy-mm-dd, quite a confusing thing the way Mysql handles dates but the end result of a date like 2030-05-12 is 2012-05-30.

mysql query in between dates not working

I have the following mysql table:
tasks:
=====================
tid
status
desc
duedate
And i have the following records in that table:
records
===========================
1
active
Test description
08/15/2014
2
active
Another description
08/31/204
I am trying to get the days that there is a task for, in that particular month. I have the following query but when i run it it gets both records but "day" is null on both of them for some reason. Can someone please help me with this.
MYSQL QUERY
====================
SELECT DATE_FORMAT(due_date,'%d') AS day FROM tasks WHERE due_date BETWEEN '08/01/2014' AND '08/31/2014'
Try:
SELECT DAY(due_date) AS day
FROM tasks
WHERE due_date >= '2014-08'
AND due_date < '2014-09';
DAY() is a better function for what you want and I prefer using >= and < than BETWEEN for date comparisons, as it allows you to specify precise ranges more easily. Here, for example, you don't need to know the number of days in the month.
I have also used the default date format, which is preferable. If you need the, in my opinion, cray American date format, use DATE_FORMAT() in your SELECT.
This will only work with DATE, DATETIME and TIMESTAMP columns, which is how your due_date should be stored, preferably DATE.
UPDATE
To convert the VARCHAR column to DATE run:
UPDATE tasks SET due_date=STR_TO_DATE(due_date,'%m/%d/%Y')
Then change the type. Also remember to change your INSERT statements to use the default format.
You've got to convert those "date" strings to proper date values with STR_TO_DATE:
SELECT
DAY(STR_TO_DATE(due_date,'%m/%d/%Y')) AS day
FROM tasks
WHERE
STR_TO_DATE(due_date, '%m/%d/%Y')
BETWEEN STR_TO_DATE('08/01/2014' '%m/%d/%Y')
AND STR_TO_DATE('08/31/2014', '%m/%d/%Y')
else you're comparing strings instead.
Note:
It would be better to use a proper DATE or DATETIME column instead.
With the current VARCHAR format MySQL is unable to use indexes. That's very bad for performance.
You can convert your data by adding another column to your table:
ALTER TABLE tasks
ADD COLUMN new_due_date DATE;
Then you use an UPDATE statement to fill this new column
UPDATE tasks
SET new_due_date = STR_TO_DATE(due_date, '%m/%d/%Y');
If you don't need your old column anymore then you can delete this column and modify the new column to have the name of the old one. Then you will have your table with all your data in a DATE column.

how to get the data using from date and to date in sql server?

i need to retreive data from database with the condition from date to to date using between query,
my query is,
select * from Master where Date between '01-08-2013' and '30-08-2013'
but it retreive all data from the table...
i need only data with in that date..
i tried another one like,
select * from PatientMaster where EntryDate >= '01-08-2013' and EntryDate<= '30-08-2013'
how its posible..
whats wrong with my query...
sorry im very bad in english...
thank you in advance...
A date string has the syntax YYYY-MM-DD and not DD-MM-YYYY
select * from Master
where `Date` between '2013-08-01' and '2013-08-30'
for that you can use
select * from Master where Date >='01-08-2013' and dateadd(dd,1,'30-08-2013')
You have to convert your strings to dates. This page shows you how to do it in mysql, which is what you have tagged. For sql server, which is in your subject line, use this page.
Then you do a slight modification of your 2nd attempt. Instead of
and EntryDate <= the end date
you want
and EntryDate < the day after the end date
That takes care of any time components. It might not matter in your case, but it's a good habit to get into.
You'll be looking for an query that works with your format? (dd-mm-yy)
CAST to the desired format!
http://www.w3schools.com/sql/func_convert.asp
105 = dd-mm-yy
SELECT * FROM Master
WHERE CONVERT(date, Date, 105) BETWEEN '01-08-13' and '30-08-13'
Be conscious with regards of the choice of data type for date Columns,
with or without time, day or year first etc. and please do not use varchar
for dates...
know that it CAN be confusing to call a date column for only Date...
be consistent with high/lower case.