Query Time range between Dates using DATETIME mysql - mysql

I have a database table that has fields as such :
TIME(Datetime) Update_ID
2013-11-25 05:00:14 XC3
2013-11-25 06:00:13 XC4
2013-11-25 06:00:19 XC5
2013-12-25 23:00:14 XC6
2013-12-25 24:00:00 XC7
So assuming i want to find a trend on the updates to know which period of the day has the a particular number of updates, what i initially think of is doing something like this :
SELECT COUNT(TIME) FROM table WHERE TIME between '06:00:00' and '12:00:00'
But this doesn't work because i think since the date is not added with the time, a default value for date is added(some date around 1970). If, i add the beginning and enddate in my query, i am afraid it won't give me the results i need.

Use
WHERE HOUR(TIME)...GROUP BY DAY(TIME)
in case you have more than 1 day

You are correct, the problem is that when you do not specify the date, a default one is added.
You can use the EXTRACT function to extract the time from a date, like this:
SELECT COUNT(*)
FROM mytable
WHERE EXTRACT(HOUR_SECOND from TIME) between 60000 and 120000
Note that the time portion in the condition is specified in a different format - i.e. as numbers, without colons and quotes.
Demo on SqlFiddle.

Related

How to get rows after a time of day using datetime column

I am running queries for a ticketing system. I want to extract all tickets created after 6:00PM or 18:00:00 in database/military time.
Could I use a DATEPART function or EXTRACT function?
Something like this may work:
SELECT *
FROM ticket
WHERE TIME IS AFTER 6PM
The issue is that the datetime format is as '2014-12-01 16:13:38' so I would need to specify for only the characters after the DATE section.
In MySQL, just use the hour() function:
SELECT *
FROM ticket
WHERE hour(time) >= 18;

take today's values between given hours in defined date-time format

I have to make a SELECT from a table where the field with the time is in format: y-m-d h-m-s but I have to output the values that are entered for the current day between 00:00:01 and 23:59:59. My query looks like that:
SELECT user, insert_time FROM t_users
WHERE insert_time BETWEEN '2014-06-30 00-00-01' AND '2014-06-30 23-59-59'
The problem is that I don't have to hardcode the date... only the hours interval. And to make it better to understand the current problem I will make a wrong input what is needed to be done: 'TODAY 00-00-01' AND 'TODAY 23-59-59' which of course don't work but if there is a way to make a query that will output the today's added values, I will be grateful.
You can do this kind of calculation with PHP:
http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
or MysQL solution:
http://blog.ubiq.co/difference-between-two-dates/
Also for today entries I think you can use:
SELECTid FROM my_table
WHERE
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),'%Y%m%d000000')
AND
timestamp >= date_format(CURRENT_TIMESTAMP(),'%Y%m%d000000')

MySQL - Using a date range vs functions

I needed to know how many users registered during June and July, this is the first query I wrote:
select count(*) from users where created_at>="2013-06-01" and created_at<="2013-07-31"
Result: 15,982
select count(*) from users where year(created_at)=2013 and month(created_at) in (6,7)
Result: 16,278
Why do they return a different result? Could someone explain? Or am I missing something?
Thanks.
Both query should be equivalent, except that the first one is able to make use of an index and it should be faster, and except the case in which created_at is not a DATE but is a TIMESTAMP.
If created_at is a timestamp, you should write your first query this way:
select count(*) from users
where created_at>='2013-06-01' and created_at<'2013-08-01'
otherwise your first query will exclude all records created on 31th of July, after midnight, eg. 2013-07-31 00:00:00 will be included while 2013-07-31 09:15:43 will be not.
The reason is that your date values do not include the last day: The date constants are converted to a timestamp at midnight. You are querying between these values:
2013-06-01 00:00:00
2013-07-31 00:00:00
So only the first second of the last day is included.
Try this:
select count(*)
from users
where created_at>="2013-06-01"
and created_at<="2013-07-31 23:59:59"
Or more simply make less than the next day:
select count(*)
from users
where created_at>="2013-06-01"
and created_at<"2013-08-01" -- < 1st day of next month

SQL extra only the day instead of entire date

I have a field in my table called created_date. The date format is 2010-02-28. I just wondering is it possible to do a mysql statement, only return the day instead of the entire date. eg. 28
SELECT
day(created_date)
FROM
table
This above query throw me error, is there a way i can do similar stuff?
cheers
Use MySQL built-in function called DAYOFMONTH
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYOFMONTH()
From Docs,
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
MySql EXTRACT function extracts day, month or year from a given date.
select extract(day from created_date) as created_day from table
you can fetch the whole date in your format and display only the required field that is date by using this
date("j", strtotime(date('Y-m-d')) );

How to insert just year and month into date field?

I have a column called table_date which currently I am using now() to insert the current date (2011-02-23). I know I can manipulate this with sql/php to show me year and monthname. However, I want to know if it's possible to just insert into table_date the current date as year-month like this 2011-02? Thanks
A DATE field is always going to be a full date, and as far as I know, it is also always required to specify a full date.
It might be easiest to use 01, like 2011-02-01 for February 2011.
Obviously, you can format the output of a DATE field to your liking when querying it:
SELECT DATE_FORMAT(fieldname,"%Y-%m");
If you insert something like 2010-10 into a DATE column, mysql throws a warning and inserts 0000-00-00. If you do not want to specify a certain day, you can insert something like 2010-10-00. Pay attention when querying for "all entries in October 2010" since WHERE date >= '2010-10-01' will not return 2010-10-00.