I am somewhat familiar with the MySQL DATE and DATE_FORMAT.
However, when running a query for a date range, using INTERVAL - 30 DAY, am either not getting the correct output or it throws an error. Looking further into the table, realize that each row for date_buy is in this type of format:
Mon 09 September 2015, etc. I have tried to use PHP date('D d F Y') instead of DATE(NOW()) and such, but am unable to resolve the issue. Is there another known workaround for this?
And yes, have tried a BETWEEN date_buy_x and date_buy_y. It returns nothing even though there are records in the table.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy >= $date - INTERVAL 30 DAY
GROUP BY date_buy
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy BETWEEN date_buy_x && date_buy_y
GROUP BY date_buy ORDER BY date_buy DESC
UPDATE
SO, based upon the suggestions, and looking further into the DATE_FORMAT and CURDATE parameters, have been able to display the data. The ORDER BY must also be in DATE_FORMAT(date_buy, '%a %d %M %Y') to output correctly. However, the issue still remains regarding INTERVAL 30 DAY parameter.
WHERE datebuy <= DATE_FORMAT(CURDATE() - INTERVAL 30 DAY,'%a %d %M %Y')
GROUP BY date_buy ORDER BY DATE_FORMAT(date_buy, '%a %d %M %Y') DESC
This is bringing back data but still way out of range, which makes me think it is not accepting the INTERVAL request.
Sat 22 November 2014 (50 sales)
Thu 18 December 2014 (50 sales)
Thu 22 January 2015 (20 sales)
Sun 25 January 2015 (20 sales)
Mon 06 April 2015 (25 sales)
Sun 12 April 2015 (25 sales)
Mon 03 August 2015 (10 sales)
When the output should only be:
Mon 03 August 2015 (10 sales)
You could conceivably do:
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= $date - INTERVAL 30 DAY
GROUP BY date_buy
Unless $date is also in this string format, in which case you need to use the STR_TO_DATE function on it too.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= DATE(STR_TO_DATE($date, '%a %d %M %Y')) - INTERVAL 30 DAY
GROUP BY date_buy
how about date_sub(),use date_format to same format when query
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE_FORMAT(DATE(date_buy,'%Y %m %d')) >= DATE_FORMAT(DATE_SUB(DATE($date,INTERVAL 30 DAY),'%Y %m %d'))
GROUP BY date_buy
Related
I'm using the function yearweek in a select query.
My question = when the date = 2017-02-13 || yearweek = 201707. How can I get a space between 2017 07
You could use DATE_FORMAT:
CREATE TABLE tu(datum DATE);
INSERT INTO tu(datum) VALUES ('2017-02-13');
SELECT datum, DATE_FORMAT(datum, '%X %V'), YEARWEEK(datum)
FROM tu;
Output:
13.02.2017 00:00:00 2017 07 201707
Rextester Demo
%X Year for the week where Sunday is the first day of the week. Used with %V
%x Year for the week where Monday is the first day of the week. Used with %V
I have a date column in the format
25 Mar 2017 07:19 pm
I want to select date in the format dd/mm/yyyy
I have tried: cast(date as DATE), STR_TO_DATE(date, '%d/%m/%Y') but everything returns null
any help will be appreciated.
Use
select str_to_date('25 Mar 2017 07:19 pm', '%d %M %Y')
The format specified has to match the input pattern. As the month name is included, use %M.
You can parse the date using following format:
SELECT STR_TO_DATE('25 Mar 2017 07:19 pm', '%d %M %Y %h:%i %p');
Here's the SQL Fiddle.
Hi all i want to select from my table all records from last 24h , 7days , 14 days ....( my sql timespan format is date("l, M d Y, h:i:s A")) so when i try to receive the data from last 24h is not working for me any help plz
exemple
if i get all date from
$result = mysql_query("SELECT * FROM stats");
result is Day,Date,time Count
Saturday, Feb 27 2016, 02:28:59 PM 27191 Saturday, Feb 27 2016, 03:28:05 PM 28659 Saturday, Feb 27 2016, 04:27:26 PM 30138
so i try like that and not working any help plz
$result = mysql_query("SELECT * FROM stats WHERE timespan >= now() - INTERVAL 1 DAY");
SELECT * FROM stats WHERE STR_TO_DATE(timespan,"%W, %b %d %Y, %h:%i:%s %p") >= date_sub(now(), INTERVAL 1 DAY)
STR_TO_DATE will convert your the string into date format (Y-m-d H:i:s) and then you can compare it with the date returned by now()
This should work. Make sure "timespan" exists and that it is a date. According to your example you should test on the "date" field.
I have an SQL table with a field "time_created" in the form "Wed, 19 Aug 2015 03:58:00 -0600".
I need to check this field against today and perform a where statement according to it, as in
SELECT * FROM table WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_created) >= 3600*24*30
to select records where time_created is older then a month.
Problem is UNIX_TIMESTAMP doesn't understand formats as "Wed, 19 Aug 2015 03:58:00 -0600". Any other way?
Try:
SELECT *
FROM table
WHERE DATE(NOW()) > DATE_SUB(STR_TO_DATE(time_created, '%a, %e %b %Y %T'), INTERVAL 1 MONTH)
This will find all records where the current date (DATE(NOW())) is bigger than time_created subtract ` month.
I have a date in this format.
08 april 1989
02 December 1984
13 January 1986
I would like to sort the results using month and day ,which has column "dateofb" and i sorting is like
13 january 1986
08 april 1989
02 december 1984
I have used the below code which doesn't work fine ,
$sel = $db->query("select * from biography where dateofb >= (CURDATE() - INTERVAL 90 DAY) order by dateofb desc limit 0,3");
I would like to display the 3 sorted results coming 90days.
Is it possible to convert the dateofb column to DATE column type? This would allow you to do what you're looking for. The format you have now is invalid and would need to be converted via STR_TO_DATE(dateofb, '%d %M %Y')
Example:
$sel = $db->query("select * from biography where STR_TO_DATE(dateofb, '%d %M %Y') >= (CURDATE() - INTERVAL 90 DAY) order by STR_TO_DATE(dateofb, '%d %M %Y') desc limit 0,3");
^- See how gross that looks? If your dateofb was a DATE column, you could just do:
$sel = $db->query("select * from biography where dateofb >= (CURDATE() - INTERVAL 90 DAY) order by dateofb desc limit 0,3");
^- which is your original query.
If your "dateofb" field is a DATE or DATETIME:
SELECT
*
FROM biography
WHERE dateofb >= (CURDATE() - INTERVAL 90 DAY)
ORDER BY MONTH(dateofb), DAY(dateofb) ASC
LIMIT 0, 3