mysql: change datetime format when select * from table - mysql

I need to query and fetch a row in a table and change the default datetime format 2015-09-15 00:00:00 to simply Sep 02 2015. Any idea how to achieve this?
I tried
SELECT
*
from tablename
where id=0;
SELECT
DATE_FORMAT(date, '%b %d %Y')
FROM tablename.
It returns two tables.

This should work:
select sub.comments_id, sub.comment, date_format(sub.date, '%b %d %Y'), sub.views
from (select comments_id, comment, date, views from tablename where id=0) as sub;
The power of MySQL is in sub selection, take advantage of it.

Use the following query:
select date_format(str_to_date(date, '%Y-%m-%d %H:%i:%s'), '%b %d %Y') from tablename;
str_to_date converts string representation to date and date_format gets you the required formatted string.

Related

SQL get dates later than specified date

I'm working on a database that stores a date column in a human-readable format. This seems to make it tricky to work out rows where a given date is later than.
The format is stored like 20 November, 2018
I'm trying to return all rows where the collection_date is later than 5 November, 2018
I've tried the following query which throws an error.
SELECT *
FROM `orders`
WHERE collection_date >= CONVERT(datetime, '20181105')
This throws the following error message:
Here's my DB info:
UPDATE 1:
I'm trying the following query. None of the previous queries have worked so far, all producing 0 rows:
SELECT *
FROM `orders`
WHERE STR_TO_DATE('collection_date', '%d %M, %Y') >= 2018-11-05
This also does not work
Actually... you have to apply STR_TO_DATE on the collection_date column because that is where the human readable dates are. As for the input date, just specify it in yyyy-mm-dd format:
SELECT *
FROM `orders`
WHERE STR_TO_DATE(collection_date, '%e %M, %Y') >= '2018-11-05'
In MySQL, you want STR_TO_DATE():
SELECT o.*
FROM `orders` o
WHERE collection_date >= Str_To_Date(datetime, '%e %M, %Y');
CONVERT() with this syntax is a SQL Server function.
The value comes first in the convert() function
WHERE collection_date >= CONVERT('2018-11-05', date)
Demo
You can try this using STR_TO_DATE() function
SELECT *
FROM `orders`
WHERE collection_date >= STR_TO_DATE('20181105', '%Y%m%d')
This query worked for me:
SELECT *
FROM orders
WHERE STR_TO_DATE(collection_date, '%d %M, %Y') >= '2018-11-05'
AND collection_time = '4:00 PM'
ORDER BY `orders`.`collection_date` DESC

MySQL Date_Format with Month() in query

I need to extract the month from the date in the database but I need to format it to became a word-written month.
Sample Date in the Database
2017-11-22
Month Query
SELECT MONTH(createdAt) FROM tbltest
The output of this is the numerical month of the createdAt ex: 11
Month with Date_Format Query <----- not working
SELECT DATE_FORMAT(MONTH(createdAt), '%Y-%b-%d') as title
FROM tbltest
Output that I am looking for
Nov
SELECT DATE_FORMAT(createdAt, '%b') as monthShortName
FROM tbltest;
Pls refer : https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php
You may try MONTHNAME function
SELECT MONTHNAME(createdAt) FROM tbltest
For short month name you can use
select DATE_FORMAT(createdAt, '%b') from tbltest

Date with date_format is sorted on alphabetical order

I am trying to SELECT the date from my database in the date_format '%e %M %Y':
SELECT date_format(date, '%e %M %Y') AS date FROM table
The output of the date will be like: 1 january 2016.
When I add an ORDER BY to the query I the date will not sort on the date, but it sorts on alphabetical order.
Does someone know how I can fix this problem and sort column: date on the date order?
Here is my full statement:
SELECT date_format(date, '%e %M %Y') AS date FROM table ORDER BY date DESC
You are using a date as alias for column name. Just use another one:
SELECT date_format(date, '%e %M %Y') AS formatted_date
FROM table
ORDER BY date DESC;

Convert a string to date (special format) in MySQL

I have a column 'birthDate' which returns a string value of [Day of the Week], [Month] [Day], [Year]
(e.g. Monday, December 30, 2013)
I am using SELECT STR_TO_DATE(birthDate, '%m/%d/%Y') but it returns a null value.
Any ideas?
SOLUTION
SELECT STR_TO_DATE(birthDate, '%W, %M %d, %Y')
You're showing one format of your date ([Day of the Week], [Month] [Day], [Year]) but using another in STR_TO_DATE ([Month]/[Day]/[Year]). You need to provide STR_TO_DATE with the format your column is currently in:
SELECT STR_TO_DATE(birthDate, '%W %m %d %Y')
If you want that result to be in a new format you can then use DATE_FORMAT():
SELECT DATE_FORMAT(STR_TO_DATE(birthDate, '%W %m %d %Y'), '%m/%d/%Y')

mySql: convert date from Jan 01, 2000 to 2000-01-01

I am trying to 'normalize' some data from a column that has two date formats, one like "Mon dd, yyyy and another "YYYY-mm-dd". I would like to convert all of the first into the second format, and then change the column type to date.
I imagine it is something like this:
UPDATE table SET
`thedate` = DATE_FORMAT(`thedate`, '%Y-%m-%d')
WHERE `thedate` LIKE '%,%'
but the DATE_FORMAT is the wrong function, I think.
Any ideas?
thanks.
I think I got it.
SELECT date_format( str_to_date( thedate, '%M %d, %Y' ) , '%Y-%m-%d' )
newdate, thedate
FROM donor
WHERE `thedate` LIKE '%,%'