I have all my data stamped with a field- dated which is in the format Y-m-d H:i- i would like to cast this field using mysql to represent the hour of the day so- 2011-11-21 15:28:05 would become 15- i have been reading the docs but it doesnt state whether or not you can specify a format
is this possible?
The idea is, I can count how many rows are stamped for each hour then display this in a graph.
this has been my working so far
SELECT CAST(dated AS TIME) FROM `posts` WHERE CAST(dated AS DATE) = CAST('".$start_date."' as DATE) AND type = '' ORDER by dated ASC;
Use date_format:
SELECT count(*), date_format(dated, "%Y:%m:%d %H") AS hour
FROM posts
GROUP BY hour
Or to get just the hour, regardless of day:
SELECT count(*), HOUR(dated) AS hour
FROM posts
GROUP BY hour
You can simply use HOUR() function to get the hour
Related
I have a table that one of its column, named 'date', is of type DATETIME. I need to select all the rows that their date is the last minute of a month, for example:
31-12-17 23:59:00
30-11-17 23:59:00
How can I achieve this in one query?
You could use LAST_DAY to get the last day of the month and DATE_FORMAT to get the time to compare.
SELECT * FROM <table_name>
WHERE DATE_FORMAT(LAST_DAY(<date_time_col>),"%d")=DATE_FORMAT(<date_time_col>,"%d")
AND DATE_FORMAT(<date_time_col>,"%H:%i")='23:59';
Detailed Explanation :
So, basically, to get the correct row we need to get the last day of the month AND last minute of the day
LAST_DAY will help use to get the last day of the month for the given date-time. And DATE_FORMAT will help to get the date. Now, we will combine them together, to get the last date of the given date-time.
DATE_FORMAT(LAST_DAY(<date_time_col>),"%d")
Above will return 29, if last day of the month is 29-02-2018.
Now, we need to check, if given date-time has last minute of the day ? We can again make use of DATE_FORMAT to extract time from the given date-time. Here, we will only concentrate on hour and minute (as per OP question). So, it should be
DATE_FORMAT(<date_time_col>,"%H:%i")
Above will return 23:59, if given date-time is 29-02-2018 23:59:00.
You can use LAST_DAY to get the last day of each month:
SELECT LAST_DAY(mydate)
returns:
2031-12-31
2030-11-30
Then use the STR_TO_DATE in order to get the last minute of the last day of each month:
SELECT STR_TO_DATE(CONCAT(LAST_DAY(mydate),
' ',
'23:59:00'),
'%Y-%m-%d %H:%i:%s') AS last_min
returns:
last_min
--------------------
2031-12-31T23:59:00Z
2030-11-30T23:59:00Z
2030-11-30T23:59:00Z
You can now use last_min to compare with your actual datetime value.
If you want to get records whose datetime falls within the last minute interval, then you can additionally use DATE_ADD to get the next minute of the above datetime values:
SELECT DATE_ADD(last_min,
INTERVAL 1 MINUTE) AS next_min
returns:
next_min
---------------------
2032-01-01T00:00:00Z
2030-12-01T00:00:00Z
2030-12-01T00:00:00Z
Using the above expressions you can build a predicate that checks for dates within the desired interval.
Demo here
SELECT * FROM table WHERE DATE(date) = LAST_DAY(date) AND HOUR(date) = 23 AND MINUTE(date) = 59;
Since this query won't use any index, it might be slow in a large table.
A more efficient solution could be as follows:
SELECT * FROM mytable WHERE mydate IN
(
'2017-01-31 23:59:00',
'2017-03-31 23:59:00',
'2017-04-30 23:59:00',
'2017-05-31 23:59:00',
'2017-06-30 23:59:00',
'2017-07-31 23:59:00',
'2017-08-31 23:59:00',
'2017-09-30 23:59:00',
'2017-10-31 23:59:00',
'2017-11-30 23:59:00',
'2017-12-31 23:59:00'
)
OR mydate = '2017-03-01 00:00:00'- INTERVAL 1 MINUTE;
The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'
I put together a query for determining whether the time difference between the current date and a record from a database is exactly a month or more apart. I am comparing now() to a created_at column, which is a timestamp.
EX:
6-12-2014,
7-12-2014
AND
5-12-2014,
7-12-2014
Should be considered to be a desirable results.
SELECT count(*) FROM `subscriptions` WHERE
DATE_ADD(CAST(created_at as DATE),INTERVAL TIMESTAMPDIFF(MONTH, created_at, now()) MONTH) = CAST(now() as DATE);
However the query appears to not return all desired results. It returns 2-28-2014 and 7-28-2014, however it does not pull up 6-28-2014. Is there a better way of doing this than the solution I came up with?
Are you looking to count dates that are on the same day of the month as the current date? If so, try the DAYOFMONTH function:
SELECT COUNT(*)
FROM subscriptions
WHERE DAYOFMONTH(created_at) = DAYOFMONTH(NOW())
I am writing a Python code where I need to use MySQLdb to retrieve data from a MySQL database. A part of the original database looks like this:
I used this command
SELECT TimeStamp,Pac
FROM SolarData
WHERE DATE(`TimeStamp`) = CURDATE()
GROUP BY HOUR(TimeStamp);
to group the data by hour, but the result is not what i expected:
The Pac number shown for every hour is the same number as the first record of each hour. It's not an accumulated number for the whole hour. What I need is an accumulated number of the whole hour.
That's because MySQL is like your alcoholic uncle when you don't use GROUP BY by the ANSI standard. You probably want:
SELECT HOUR(TimeStamp) AS Hour,
SUM(Pac) AS Pac
FROM SolarData
WHERE `TimeStamp` >= CURDATE()
AND `TimeStamp` < CURDATE() + INTERVAL 1 DAY
GROUP BY HOUR(TimeStamp);
It would be helpful to see the desired result you're looking for. Until then, the above query is just a guess based on group the data by hour. For future reference, use SQL Fiddle to post your table structure/data.
"How do I decorate the code so that the hour format can have the date on it as well like this 2014-01-14 07:00"
All of Hour belong to today (CURDATE()), and second part is always ':00', so following query might help you. Could try this?
SELECT CONCAT(CURDATE(), ' ', Hour, ':00'), Pac
FROM (
SELECT HOUR(TimeStamp) AS Hour,
SUM(Pac) AS Pac
FROM SolarData
WHERE DATE(`TimeStamp`) = CURDATE()
GROUP BY HOUR(TimeStamp)
) x;
The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1