How can we change date format in MySQL - mysql

I want to change date format in mysql.I tried this code
DATE_FORMAT(STR_TO_DATE(mydate, '%d.%m.%y'), '%Y-%m-%d')
But its not hep me.
My date comes from as string like this:
DD-MM-YYYY
I want to change it to this:
YYYY-MM-DD
How can I achive this with mysql?
Thanks

Would:
STR_TO_DATE(mydate, '%y.%m.%d')
work then because when your are creating the date from the string you are implicitly giving it a format. Then just call it in a different format.

SELECT str_to_date(table_column_name,'%d-%m-%Y') FROM `table_name`

Related

How to Select Data Between Two Dates and Times in SQL Server?

why this query gives me the wrong output although I am trying to print data between '01-May-2022' and '30-May-2022';
why is this query giving me apr data?
It appears that you are storing your dates as text. You should not be doing this, and the best long term fix is to make datee a proper date column. As a short term fix, you may use STR_TO_DATE:
SELECT datee
FROM customer_shopping_details_tbl
WHERE STR_TO_DATE(datee, '%d-%b-%Y') BETWEEN '2022-05-01' AND '2022-05-30';
Thank you all ....
I did a mistake while saving the date, I had saved the date in the string format. That is the reason that the query does not show proper output
What I learned/point to remember
-MySQL date format is yyyy-mm-dd
-Save your date in the form of date NOT IN String
go through the comments for the better idea
I have saved my data in the form of String so i have fired the following query and is working now
Good evening,
Maybe this could help:
SELECT login,datetime FROM log where datetime between
to_date(’01-jan-2004,’mm/dd/yyyy’) and
to_date(’21-jan-2004′,’mm/dd/yyy’)
order by datetime DESC;

Converting Date to String and then to Date again in mysql .Combining 2 functions in mysql

have column which is of type date having default format as YYYY-MM-DD. I want to get date in format DD-MM-YYYY but as date type only. I would like to know how is it possible? I am combining 2 functions together as shown below but i am not getting the desired result
STR_TO_DATE(DATE_FORMAT(emp.doj, '%d-%m-%y'),'%d-%m-%y') as doj
Please dont close this questions as its a different one not asked before thanks!!!
DATE_FORMAT(emp.doj,'%d-%m-%Y') as doj
You have not need to use STR_TO_DATE. use only DATE_FORMAT. It should be work.

change the format of the date

I have default date from database like '01/02/2015'.
How can I output date in '1/2/2015' format?
You can use the DATE_FORMAT function with the formating string as %c/%e/%Y to get the format you want.
DATE_FORMAT(CURDATE(),'%c/%e/%Y')
Here are more formats and information about them.
select DATE_FORMAT(YOUR_DATE,'%e/%c/%Y') from your_table
references : w3schools

how do I convert date type to hh:mm in mysql?

I got in my table two types:
hour_Event in type date
date_Event in type datetime
I would like hour_Event to be formatted to hh:mm
and date_Event to be dd/mm/yyyy
I use PHPMyAdmin and im new with MySQL so I dont know how to change the format and for what.
How do I do it?
**DATE_FORMAT(hour_Event,'%h:%i')** will give you hour_Event in hh:mm
and
**DATE_FORMAT(date_Event,'%d/%m/%Y')** will give you date_Event in dd/mm/yyyy
You could change the format using php before you insert data or after retrieving themfrom your database according to your needs.You could use DateTime / strtotime() & date(). In most cases a single timestamp field in your table is enough for an event.
Have a look here:
Convert one date format into another in PHP
Also you could format your field according to your needs with mysql for example:
DATE_FORMAT(tables.field, '%d-%m-%Y %H:%i:%s') AS 'new_name'
Have a look about it here:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

How to convert yyyy/mm/dd to dd/mm/yyyy?

I would like to convert yyyy/mm/dd to dd/mm/yyyy format.So, I am using strtotime() function.but when I get zero date like(0000/00/00) from mysql. It will be showing wrong date like(01-01-1970).
I need exact string like 0000/00/00 to 00/00/0000. any other function are there?
Try MySQL's DATE_FORMAT() function
SELECT DATE_FORMAT(dateColumn,'%d/%m/%Y') AS dateColumn FROM table...
Try to use strftime
strftime("%d-%b-%Y", strtotime($value['your_date']));