Date Format in MYSQL - mysql

I have a column date in MYSQL, i want to display date in day Month year (day of week) format.
For Example :
if date value is 2011-01-01 than it should display as 1 Jan 2012 (Sun). How should i acheive this ?

Refer this MySQL 5.1 Reference Manual for more info
Date and Time Functions

You can use this:
EDITED:
SELECT DATE_FORMAT('2012-01-01', '%d %b %Y (%a)'); will give you 1 Jan 2012 (Sun)
%d = 01;
%b = Jan;
%Y = 2012;
%a = Sun;
For a detailed date formats see: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Hope this helps.

you can use DATE_FORMAT in mysql:
DATE_FORMAT(column_name, '%e %b %Y (%a )')
"%m" will give numeric value like '01','02' for Jan, Feb you should use "%b".
and also "%d" will return 2 digits like '01','02' but "%e" will return '1','2'.
check out this link for more details, you can change as per your choice.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

try this
SELECT DATE_FORMAT(Date, '%d %b %Y (%a )') from table;

Related

convert full date string to date mysql

I have database which contains string like this
22 Jan 2019 11:03
I would like to convert this string to date so I apply this query
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d-%m-%Y') ,'%d-%m-%Y');
but I get a null result
All you have to do is change small letter m to big letter M in your str_to_date function.
select STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y');
so the final query would be:
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y') ,'%d-%m-%Y');
Here is a demo
SELECT STR_TO_DATE('22 Jan 2019 11:03','%d,%m,%Y');
Basically you first need to understand the values to pass in STR_TO_DATE() and DATE_FORMAT() functions
STR_TO_DATE(my date string , current format of my date string)
Now what you are not doing right is that in STR_TO_DATE() you are passing the format '%d-%m-%Y' , this format says that the input string has hyphen separated date month and year values which is not true.
Now in your case the actual format of your date string is the following
'%d %M %Y %h:%i'
STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i')
%d - day
%M - month
%Y - year
%h - hour
%i - minute
For more info on formats click
Now that we have a complete valid value from string to date including hour and minutes, we can convert this date into any of our required format using correct parameters
SELECT DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i') ,'%d-%m-%y %h:%i');
You can try out various examples here
https://www.mysqltutorial.org/tryit/query/mysql-str_to_date/#1

SQL: Get the beginning date of the month from a Month_year name column

I have a date data as below:
Monthyear
Jan 2018
Feb 2018
Mar 2018
I want to convert this into this format:
Monthyear
01-01-2018
01-02-2018
01-03-2018
That is, as a date with the first date of the month.
I tried casting the above and it doesnt work.
Kindly help me out.
You can use STR_TO_DATE:
SELECT STR_TO_DATE(CONCAT('1 ', Monthyear), '%e %b %Y')
e.g.
SELECT STR_TO_DATE(CONCAT('1 ', 'Jan 2018'), '%e %b %Y')
Output:
2018-01-01
The reason for CONCATing a '1 ' to the string is to avoid issues with SQL NO_ZERO_DATE mode (this is discussed in the manual entry for STR_TO_DATE).
use str_to_date()
select str_to_Date(concat('01',Monthyear),"%d %b %Y")

date String "08th August 2017" to date conversion in mysql

I am using MySQL, I have a column value like this 08th August 2017.
I want to convert it into a date like 8-8-2017. I have tried MySQL date_formate() function, STR_TO_DATE() function but it return a null value.
How can do this task ?
SELECT STR_TO_DATE('08th August 2017', '%D %M %Y') works fine for me
You need to use the %D %M %Y format for STR_TO_DATE
SELECT STR_TO_DATE(datefield,'%D %M %Y')
FROM yourtable
Input
08th August 2017
Output
2017-08-08
SQL Fiddle: http://sqlfiddle.com/#!9/701e66/5/0
First convert your date string into a DATETIME value and then use that in a DATE_FORMAT() to get the date into whatever format you want
SELECT DATE_FORMAT(STR_TO_DATE('08th August 2017', '%D %M %Y'),'%d-%m-%Y') AS
test;
You can use this code in mysql.
I am just giving example how to use.
select STR_TO_DATE("08th August 2017", "%d %m %y") from demo
where demo is the demo table.

How to convert the UNIX TIME into SQL datetime format

select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
The output of the above sql query gives a table with a column named "Devices" with number of devices. I want the time which is one of the attributes of measure_tab should also get displayed in another column of the output table with the UNIX TIME which is 1375243200 in this query converted into the SQL datetime format which is Thu Aug 1 00:00:00 UTC 2013.
Can someone help me out?
Thanks
You can use function as below:
select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%a %b %d %H:%i:%s UTC %Y');
output will be:
'Wed Feb 05 05:36:16 UTC 2014'
In your query
select COUNT(DISTINCT devices) AS "Devices",
FROM_UNIXTIME(measure_tab.time,'%a %b %d %H:%i:%s UTC %Y') as d from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
For more info you can check documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
You can see sql fiddle:http://sqlfiddle.com/#!2/a2581/20357
In Mysql you can use from_unixtime() function to convert unix timestamp to Date:
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= from_unixtime(1375243200) and
measure_tab.time < from_unixtime(1375315200);
you could use FROM_UNIXTIME inside DATE_FORMAT, but luckily,
FROM_UNIXTIME also accepts a format string, so you could just use it
by itself
Like this
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x')
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
As detailed in the other answers, FROM_UNIXTIME is the function you are looking for. Please be aware that this implicitly takes into account the local time zone setting on the machine running MySQL.
There's lots of useful information here:
Should MySQL have its timezone set to UTC?
if you need to find out how to check/set the time zone.

MySQL:How to sort date with RFC112 format?

For example, I have a column named date on my table post and I want to sort it in ascending.
On my date column I fill it with RCF112 format, eg: Sun, 22 APR 2012 5:21:22.
First I begin with this command:
SELECT *
FROM post
ORDER BY date ASC
But the result appears to be incorrect because it was sorted according to its string, eg. the Sun, 15 APR 2012 will be older than Wed,11 APR 2012 because "Sun" starts with 'S' which is in alphabetic ahead 'W', so the "Sun, 15 APR 2012" appears first.
How to correct this command?
You need to parse the string as datetime to be able to sort it correctly.
Using your format, you can try something like this:
STR_TO_DATE('Sun, 22 APR 2012 5:21:22', '%a, %e %b %Y %h:%i:%S')
which creates the date 2012-04-22 05:21:22.
So, your query should look something like this:
SELECT *
FROM post
ORDER BY
STR_TO_DATE(date, '%a, %e %b %Y %h:%i:%S')
ASC
As others might have already suggested, you could use the datetime field type and format the date in the select (date_format http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format) to fit your requirements.
SELECT *
FROM post
ORDER BY STR_TO_DATE(datestr, '%a, %d %b %Y %T') ASC