Convert dd.mm.yyyy to yyyy-mm-dd in SQL - mysql

I need to convert a date in format dd.mm.yyyy to yyyy-mm-dd
For example, if the date is 04.12.2020, i want it to be like 2020-12.04(which is 4th december)
But the command giving me the output as 2020-04-12( which is 12th April)
select convert(date,'04.12.2020' )

I think you just want str_to_date():
select str_to_date('04.12.2020', '%d.%m.%Y')

It sounds like you have a date as a string? (ie. the "04.12.2020").
MySql has a STR_TO_DATE() function:
select str_to_date('04.12.2020', '%d.%m.%Y')
That should convert it to an actual date datatype.
If needed an actual date can then be formated in which ever way you want or just visualized depending on what you're trying to do with DATE_FORMAT():
select date_format(date, '%Y-%m-%d')

Related

How to convert a date string to mysql date format for calculating date diff using mysql query

I have a column where a date store in ddmmyy format (e.g. 151216). How can I convert it to yyyy-mm-dd format (e.g 2016-12-15) for calculating a date difference from the current date? I try using DATE_FORMAT function but its not appropriate for this.
If you want to get the date difference, you can use to_days() after converting the string to a date using str_to_date():
select to_days(curdate()) - to_days(str_to_date(col, '%d%m%y'))
or datediff():
select datediff(curdate(), str_to_date(col, '%d%m%y'))
or timestampdiff():
select timestampdiff(day, str_to_date(col, '%d%m%y'), curdate())
You can use the function, STR_TO_DATE() for this.
STR_TO_DATE('151216', '%d%m%y')
A query would look something like:
select
foo.bar
from
foo
where
STR_TO_DATE(foo.baz, '%d%m%y') < CURDATE()
Note: Since both STR_TO_DATE() and CURDATE() return date objects, there's no reason to change the actual display format of the date. For this function, we just need to format it. If you wanted to display it in your query, you could use something like
DATE_FORMAT(STR_TO_DATE(foo.baz, '%d%m%y'), '%Y-%m-%d')
To get the difference, we can simply subtract time
select
to_days(CURDATE() - STR_TO_DATE(foo.baz, '%d%m%y')) as diff
from
foo
If you wanted to only select rows that have a difference of a specified amount, you can put the whole to_days(...) bit in your where clause.
SELECT STR_TO_DATE('151216', '%d%m%y') FROM `table`
use this '%d%m%y'

How to convert date in MYSQL to correct format

My current date formats are 01/01/2013 .... DD/MM/YYYY
How can I convert them all into MYSQL dates? I'm under The impression they are in the format YYYY-MM-DD
I don't know where to start.
my problem is that the dates are being ordered in the american way whilst they are in british format :(
Use the following query.
update tbl_name set your_field_name= str_to_date(your_field_name, '%d/%m/%Y');
It will update the value of your date from DD/MM/YYYY to YYYY/MM/DD.
Then you can change your filed type to date.
You can extract each part in php and concat the dd, mm, yyyy and save it to the DB
Why not DATE_FORMAT
DATE_FORMAT(date,'%Y-%m-%d')
How to convert date in MYSQL to correct format:
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
or
SELECT t.id, DATEDIFF(STR_TO_DATE(t.carddate, '%m/%d/%Y'), CURDATE)
FROM TABLE t
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
I have a very similar situation. I converted American date format "%Y-%d-%m" to correct format '%Y-%m-%d'. This is how I did it...
update table_name set my_date = DATE_FORMAT( STR_TO_DATE( my_date, '%Y-%d-%m' ) , '%Y-%m-%d' );
The first date string format '%Y-%d-%m' needs to be how the date is currently formatted in your table. The second date string is what you want to convert it to.

select a date lesser than a particular date

I have stored the dates as string in my database.I know it is not good,but project already has been half developed before i take over and where dates were stored as string,so i was continuing the same way.
Now i want to select dates from table where date is greater than a specific date.
I tried the following query
SELECT
*
FROM
dates
where
STR_TO_DATE(date, '%Y-%m-%d') > "2014-01-01"
but it is not returning only greater values.
Please help me to solve problem.
Demo
Your dates are not in YYYY-MM-DD format. Use the right format!
SELECT *
FROM dates
where STR_TO_DATE(date, '%m-%d-%Y') > date('2014-01-01')
If you are going to store dates as strings, then the best way is in the ISO format of YYYY-MM-DD.
You should read the documentation on str_to_date() (here).
Convert everything to date and it should be fine. Now you are comparing date and string.
What type has the date? I'd prefer a ' instead of " for strings in SQL. Let's assume that date is a VARCHAR or TEXT field (depending on which database you are using):
SELECT *
FROM dates
WHERE STR_TO_DATE(date, '%Y-%m-%d') > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
If date is a real DATE
SELECT *
FROM dates
WHERE trim(date) > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
Or you just convert it into a number format date_format(date,'%Y%m%d') > 20140101

Convert a obscure? date format in a sql query to a human readable one

I've got a date format in a bigint field in this format "20130314123743" - YYYYMMDDHHMMSS and i need to do a mysql query on it and get it back to the user in something like yyyy-mm-dd hh:mm:ss.
Is there a native mysql function that will take that date format and return it to something human readable?
One approach:
select cast(cast(bigintdateval as char(14)) as datetime)
SQLFiddle here.
To convert to a datetime you can use MySQL function FROM_UNIXTIME
FROM_UNIXTIME (unix_timestamp, [format ])
SELECT FROM_UNIXTIME(20130314123743, '%Y-%m-%d %h:%i:%s');
Exemple;
SELECT FROM_UNIXTIME(birthDay/100000, "%Y-%m-%d %H:%i:%s") AS date
FROM person;
source
FROM_UNIXTIME
Hope that help

STR_TO_DATE parsing of %b-%Y

I need to parse date in the format JAN-1980 to 1980-01-01 i.e. by adding the day as always being the first date of the indicated month.
How can I go about his?
something like this,
SELECT STR_TO_DATE(CONCAT(`colName`, '-01'),'%b-%Y-%d')
FROM table1
SQLFiddle Demo
SOURCE
STR_TO_DATE
Concat -01 in your date, convert to date and then format back in your desired format e.g. below:
DATE_FORMAT(STR_TO_DATE(CONCAT('JAN-1980','-01'),'%b-%Y-%d'), '%Y-%m-%d')