Convert a string to date (special format) in MySQL - 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')

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

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;

Remove leading zeros from %m and %d in MySQL date_format

The following query produces a date that looks like this 2016, 01, 02. How do I get it to remove trailing zeros from the month and day so that it looks like this 2016, 1, 2?
SELECT DATE_FORMAT(earning_created, '%Y, %m, %d') AS day, SUM(earning_amount) AS amount
FROM earnings
WHERE earning_account_id = ?
GROUP BY DATE(earning_created)
ORDER BY earning_created
You can use %c to format the month without the leading zero and %e to format the day of the month:
SELECT DATE_FORMAT(earning_created, '%Y, %c, %e') AS day, -- Here!
SUM(earning_amount) AS amount
FROM earnings
WHERE earning_account_id = ?
GROUP BY DATE(earning_created)
ORDER BY earning_created

mysql: change datetime format when select * from table

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.

MySQL IF with CONCAT AND DATE_FORMAT

I have the following statement, which returns empty values for 'label':
SELECT *,
IF (
`date_to` IS NULL,
CONCAT(DATE_FORMAT(`date_from`, '%D %M %Y'), ' ', `location`),
CONCAT(DATE_FORMAT(`date_from`, '%D %M %Y'), ' - ', DATE_FORMAT(`date_to`, '%D %M %Y'), ' ', `location`)
) AS `label`
FROM `courses`
ORDER BY `date_from` ASC
The date_from and date_to columns are both DATE types, and the second one might contain a default value of 0000-00-00.
Any idea what might be causing it?
DATE_FORMAT(cast('0000-00-00' as date), '%D %M %Y') returns NULL, therefore the entire CONCAT will also become null.
Perhaps you should also compare date_to to the value '0000-00-00' in the IF condition?