How do you convert the following date Mar 7 2017 1:26:46:886AM to 2017-03-07 01:26:00
select DATE_FORMAT(STR_TO_DATE('Mar 7 2017 1:26:46:886AM', '%b-%d-%Y'), '%Y-%m-%d');
I keep getting null
This is the answer
SELECT STR_TO_DATE("Mar 7 2017 1:26:46:886AM", "%M %d %Y %H:%i:%S")
you could use
select STR_TO_DATE('Mar 7 2017 1:26:46:886AM', '%b %d %Y %h:%i:%s%f' )
Related
I'm trying to fix the order by and group by year and month format. This is my sql query
SELECT DATE_FORMAT(created_at, "%Y %b") ym
FROM `book_summaries`
GROUP BY DATE_FORMAT(created_at, "%Y %b")
ORDER BY DATE_FORMAT(created_at, "%Y %b")
But the sql query result is this:
2018 Apr
2018 Aug
2018 Dec
2018 Feb
2018 Jul
2018 Jun
2018 Mar
2018 May
2018 Nov
2018 Oct
2018 Sep
2019 Apr
2019 Aug
2019 Dec
It seems that only the year format has been ordered but not the months in it. How can I alter my query to order by year and then months?
You can order by the minimum date in each range:
SELECT DATE_FORMAT(created_at, '%Y %b') ym
FROM `book_summaries`
GROUP BY DATE_FORMAT(created_at, '%Y %b')
ORDER BY MIN(created_at);
Note that I also replaced the double quotes with single quotes. Single quotes are the SQL standard for strings (although some database do support double quotes as a string delimiter).
I have mysql database which contain date like this value
22 Jan 2019 11:03
I want to select all records that have values less than today , so I wrote this query
select username from radcheck
where DATE_FORMAT(STR_TO_DATE(value,'%d %M %Y') ,'%d-%m-%Y') < DATE_FORMAT(NOW(), '%d-%m-%Y')
but this query return all records even those not expired .
where is my problem
SELECT username
FROM radcheck
WHERE STR_TO_DATE(`value`,'%d %b %Y %H:%i') < CURRENT_DATE
I'm trying to select data between july 2017 to jun 2018 using sql query but not getting expected output. Let's say m having data from jan 2017 up to dec 2018,
and i want to select data between july 2017 to jun 2018. how can I achieve this?
This is the data i'm having
And Expected Output is
See MySQL STR_TO_DATE() Function
SELECT * FROM <YOUR TABLE> WHERE STR_TO_DATE(CONCAT('1 ',`Month`,' ',`Year`), '%d %M %Y')
BETWEEN STR_TO_DATE('1 Jun 2017', '%d %b %Y') AND STR_TO_DATE('31 Jul 2017', '%d %b %Y');
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')
Part of my SQL query
DATE_SUB(dStartDate,INTERVAL 30 DAY) AS dEarlyBirdExipiryDate
It return dEarlyBirdExipiryDate as 2013-05-16
But I want it in 16 May, 2013 format.
What changes I should make in my query to get this?
Use DATE_FORMAT() function:
SELECT DATE_FORMAT( DATE_SUB(dStartDate, INTERVAL 30 DAY)
, '%d %M, %Y'
) AS dEarlyBirdExipiryDate;
See this SQLFiddle
You can use something like this. Em not sure but it would work with a few tweaks.
SELECT DATE_FORMAT(DATE_SUB(DATE('2007-11-30 09:00:00'), INTERVAL 1 DAY), '%d %M, %Y'); //