Get rows between two date range - mysql

I have a simple SQL query which will search for matched rows between two date range. Date format to be searched for is 'yyyymm' -> year-month.
I have written the following query but it is giving me error
SELECT *
FROM `my_table`
WHERE added_on BETWEEN EXTRACT( YEAR_MONTH FROM `added_on` )='201606' AND
EXTRACT ( YEAR_MONTH FROM `added_on` )='201706'
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '='201606' AND EXTRACT ( YEAR_MONTH FROM `added_on`
)='201706' LIMIT 0, 30' at line 1

I don't think there is a need to use EXTRACT at all in this case. Instead you can try:
SELECT *
FROM `my_table`
WHERE added_on BETWEEN '20160601' AND '20170630'

Try this query
SELECT *
FROM `my_table`
WHERE EXTRACT( YEAR_MONTH FROM `added_on` ) BETWEEN EXTRACT( YEAR_MONTH FROM '201606' ) AND EXTRACT( YEAR_MONTH FROM '201706' )
'201606' and '201706' should be user input.
If added_on column datatype is date, write EXTRACT( YEAR_MONTH FROM added_on )
If not date datatype, just write column name

Try DATE_FORMAT(date,'%Y%m') /EXTRACT(YEAR_MONTH FROM date) to achieve yyyymm
Using DATE_FORMAT
SELECT *
FROM `my_table`
WHERE DATE_FORMAT(added_on,'%Y%m') BETWEEN '201606' AND '201706'
Using EXTRACT
SELECT *
FROM `my_table`
WHERE EXTRACT(YEAR_MONTH FROM added_on) BETWEEN '201606' AND '201706'
Without using them
Add 01 to the first date and 31 to the second date.This represent the first and last days of the month
SELECT *
FROM `my_table`
WHERE added_on BETWEEN '20160601' AND '20170631'

You can use below query -
SELECT * FROM `my_table` WHERE added_on BETWEEN REPLACE(SUBSTRING_INDEX(added_on,'-',2),'-','')='201606' AND REPLACE(SUBSTRING_INDEX(added_on,'-',2),'-','')='201706';

Related

MYSQL Error : SQLSTATE[42000]: Syntax error or access violation: 1248 Every derived table must have its own alias

Can anyone tell me why i keep getting an error with this sql query ?
select hour , price , date
from
(
select Hour(c_date) as hour , avg_price AS price , c_date as date
from brc_table
where c_date >= date_sub(now(), interval 1 Week)
)
group by date
You must assing a name to the derived table, your form ( select .... )
so add eg : t ad the endo of from()
select hour , price , date
from
(
select Hour(c_date) as hour , avg_price AS price , c_date as date
from brc_table
where c_date >= date_sub(now(), interval 1 Week)
) t
group by date
In mysql, DATE is a "Reserved Word", so you cannot have a column named date, and you cannot use the word date in an alias (as with AS date.)
You should either pick some name other than 'date', or you should quote the word 'date' with back-quotes, (otherwise known as backticks,) like this:
`date`

All MySQL records from yesterday

What is an efficient way to get all records with a datetime column whose value falls somewhere between yesterday at 00:00:00 and yesterday at 23:59:59?
SQL:
CREATE TABLE `mytable` (
`id` BIGINT,
`created_at` DATETIME
);
INSERT INTO `mytable` (`id`, `created_at`) VALUES
(1, '2016-01-18 14:28:59'),
(2, '2016-01-19 20:03:00'),
(3, '2016-01-19 11:12:05'),
(4, '2016-01-20 03:04:01');
If I run this query at any time on 2016-01-20, then all I'd want to return is rows 2 and 3.
Since you're only looking for the date portion, you can compare those easily using MySQL's DATE() function.
SELECT * FROM table WHERE DATE(created_at) = DATE(NOW() - INTERVAL 1 DAY);
Note that if you have a very large number of records this can be inefficient; indexing advantages are lost with the derived value of DATE(). In that case, you can use this query:
SELECT * FROM table
WHERE created_at BETWEEN CURDATE() - INTERVAL 1 DAY
AND CURDATE() - INTERVAL 1 SECOND;
This works because date values such as the one returned by CURDATE() are assumed to have a timestamp of 00:00:00. The index can still be used because the date column's value is not being transformed at all.
You can still use the index if you say
SELECT * FROM TABLE
WHERE CREATED_AT >= CURDATE() - INTERVAL 1 DAY
AND CREATED_AT < CURDATE();
You can use subdate to indicate "yesterday" and use date() to indicate that you want records where just the date part of the column matches. So:
SELECT *
FROM tablename
WHERE DATE(created_at) = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
Here is the same question with an answer. To summarize answer for you, use subdate() as suggested by Sajmon.
subdate(currentDate, 1)
using your table it should be.
select *
from tablename
where created_at between subdate(CURDATE(), 1)
and date (now() )
use:
subdate(current_date, 1)
it's awesome for your case!
SELECT subdate(current_date(), 1)
SELECT * FROM table
WHERE created_at >= subdate(current_date(), 1)
You can use this, just put tablename and columnName (Which Contain 2021/01/09 or 2022-01-11 14:56:07 etc)
select * from (TABLENAME) where DATE(columnNAME) = TODAY - 1;

Searching date MySql

i have a row in my MySql table 'a' with values 'date'='2015-06-01' and 'hour' = 17:00:00
My problem is that i can select this row by
hour BETWEEN "16:30:00" AND "17:30:00"
but i cant by date = 2015-06-01.
I need my full statement like this:
SELECT * FROM `a`
WHERE (date = 2015-06-01)
AND (hour BETWEEN "16:30:00" AND "17:30:00")
You need quotes around the date too
SELECT * FROM `a`
WHERE `date` = '2015-06-01'
AND `hour` BETWEEN '16:30:00' AND '17:30:00'

How to count number of date between two days MySQL

On my mysql table I keep date as datetime. (eg: 2013-11-09 11:15:58), I want to write mysql Query to get number of dates to given date. can someone help me to write this..
eg:
Given date: 2014-01-19 10:15:15
On table 'date_added' has: 2013-11-09 11:15:58
is it possible to get number of dates using mysql query, like COUNT(), SUM()
I think your using TIMESTAMP to use this method
SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01') from tablename;
if you want to count following this code:
SELECT COUNT(*) FROM a1 WHERE '2014-03-01';
To find number of records on a specific date of given timestamp, use date_format or date functions on the field to filter by date.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date_format( date_added, '%Y-%m-%d' );
Alternatively, you can also use date function to extract date part from a datetime column.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date( date_added );
And if you want to count based on date part of an input datetime value then, use
select
date( '2014-01-19 10:15:15' ) dt,
count( date_added ) dt_cnt
from my_table
where
date( date_added ) = date( '2014-01-19 10:15:15' );
Refer to:
DATE(expr)
Extract the date part of a date or datetime expression
DATE_FORMAT(date,format)
Format date as specified

How to convert a mysql datetime column to just date?

Here is an explanation of how my table is set up (in sqlfiddle)
http://sqlfiddle.com/#!2/86a54d/2
Basically, I have two columns. One is a varchar and contains a name. The other is a datetime and contains a certain date with a time.
I want to select rows where the date in the datetime matches today's date. I have tried things like...
select * from alerts_to_send where date_to_send = now()
select * from alerts_to_send where date_to_send = curdate()
I'm not exactly where to go from here... Any ideas would be greatly appreciated :)
Just to re-clarify, I want to select the rows where today's date matches the date part of my datetime type column.
try this
select * from alerts_to_send
where DATE_FORMAT(date_to_send,'%m-%d-%Y') = DATE_FORMAT(now(),'%m-%d-%Y')
fiddle
You can use the date_format function for mysql
select * from alerts_to_send where date_format(date_to_send, "%m/%d/%Y") = date_format(now(), "%m/%d/%Y")
Here is reference to date_format: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
An alternative to date_format function you can convert the datetime to date:
select * from alerts_to_send where cast(date_to_send as date) = curdate()
It could be even shorter:
select * from alerts_to_send where date(date_to_send) = date(now())
But you will have problem with using index on it so better will be:
select * from alerts_to_send where date_to_send >= date(now()) and date_to_send < date(now()) + INTERVAL 1 DAY