MYSQL Date - str_to_date() function messed with year in my dates - mysql

Just changed the date from the format dd-mm-yy using str_to_date() function in MYSQL.
The column date had the date in dd-mm-yyyy format. I used following query :
UPDATE invoice SET date=str_to_date(date, '%d-%m-%Y');
and dates got converted to correct format yyyy-mm-dd but all years in my date column got changed to 2020.
Now I have two questions :
1] How can I change all 2020 year to 2011 in my date
2] What do you think, why did this happen ?

You can use a query like this:
UPDATE table SET datefield=datefield - INTERVAL 9 year WHERE records you want to change
If you leave out the WHERE clause it changes ALL records in the table.

Related

Date conversion query in SQL using STR_TO_DATE

In my table, there is a date column i.e. ORDER DATE which is in dd-mm-yyyy format, and the datatype of this date column is text. Now, I want to convert the format from yyyy-mm-dd. But the date format is not changing at all. Trying to use STR_TO_DATE but the year in the dates are coming as 2020 only. Not sure how to fix it or if there is a better way to convert the date in MySQL.
For example:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%y') AS DATE;
Result coming as 2020-09-14
Or suppose
SELECT STR_TO_DATE('20-MAR-2018','%d-%b-%y') AS DATE;
Result coming as 2020-03-20
day and month are coming properly but the year is not coming. Kindly help how to get convert the date format correctly.
Try to change your command like this below and you will get the answer:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%Y') AS DATE;
Refer to the documentation for more info about the commands.

MySQL CSV Imported Year Dates as 0018 Invalid Date

Getting an error when trying to correct the year portion of imported dates.
CSV Date Column Values were formatted
07/21/18 instead of
07/21/2018
This caused MySql to Insert Date as 07/21/0018
I was under the impression that year values in the range 00-69 were converted to 2000-2069 as stated in the documentation.
Any way to fix this? I've tried quite a few statements with no luck...
Any help appreciated
Assuming you want to just update the data in place, and it is a column of Date, DateTime or Timestamp types, you could do this:
UPDATE table SET date = date + INTERVAL 2000 YEAR WHERE YEAR(date) < 70

MYSQL Insert date today with specific format query

Hi I want to insert a data into table with the date today with a specific date format. My date in column is set to like dd-mm-yyyy but i want to insert a date in literal form like 28 October 2016
I know how to insert the current date today and also know how to select date with specific format but I'm having a little confusion with combining this two queries
For inserting current date:
INSERT INTO product_table(product_names, product_type,product_brand,product_price,product_count,product_note, product_date) VALUES ('Logitech','Mouse','Sample',100,5,'1 year warranty',CURRENT_DATE)
For selecting with specific date format
Select DATE_FORMAT(product_date, %d %M %Y) from product_table;
Thank you
If you want to store a date in a column as 28 October 2016, you'll need to make it a VARCHAR field or something.
If that's the case, I don't why you'd want to insert it like that, rather than just format it on the way out of the database?

What date time datatype i should use to insert date like March2016 in mysql?

As i am trying to insert the date 'March16' which is March 2016 in mysql table x with datatype DateTime. But this gives me error incorrect date value.
Please help me . Thank you very much
'March16' is not a correct date format. You need to do STR_TO_DATE() or DATE_FORMAT().
Read more about above two.
If you want to insert only month and date
Mysql doc
Ranges for the month and day specifiers begin with zero due to the
fact that MySQL permits the storing of incomplete dates such as
'2014-00-00'.
This means that to store the year and month only, you
can use a DATE column and put 00 instead of the day. e.g 2013-12-00.

query between dates returns rows from previous months too

I have the following where clause
AND DATE_FORMAT( l.created_on, "%d/%m/%Y" ) BETWEEN '06/02/2013' AND '07/02/2013'
where created_on is a timestamp.
Now for some reason the query returns rows from previous months as well. anyone knows why?
NOTE :
I need the date to be in that specific format
Mysql string date format follows pattern yyyy-mm-dd. Do not convert to dates if you have timestamps, just compare the timestamps.
WHERE l.created_on
BETWEEN UNIX_TIMESTAMP('2012/02/06') AND UNIX_TIMESTAMP('2013/02/07')
if created_on is already a date (datatype),
you can directly query it using,
WHERE created_on BETWEEN '2013-02-06' AND '2013-02-07'
but if date is a string, use STR_TO_DATE
WHERE STR_TO_DATE(l.created_on, '%d-%m-%Y') BETWEEN '2013-02-06' AND '2013-02-07'
UPDATE 1
since you don't want to change the format of your inputted date, then you need to format it using STR_TO_DATE
WHERE l.created_on BETWEEN STR_TO_DATE('06/02/2013','%d/%m/%Y') AND
STR_TO_DATE('07/02/2013','%d/%m/%Y')