select * from table where 1 and DATE_FORMAT(dateColumn,"%d-%m") BETWEEN "01-09" and "30-09"
This query is returning all the data. I just want the data between these dates, without taking into account the year.
In the table dateColumn is yyyy-mm-dd.
You need to do the comparison as MM-DD, not DD-MM:
select *
from table
where DATE_FORMAT(dateColumn, '%m-%d') BETWEEN '09-01' and '09-30'
Or, for this case, you could just do:
where month(dateColumn) = 9
Try this:
SELECT *
FROM table
WHERE MONTH(dateColumn) = 9
You can check if the month is September
select * from table where DATE_FORMAT(dateColumn,"%m")
= '09
If you want rows from your table where the month of your date is 9 and the day of month is between 1 and 30, you can actually do this quite easy and more explicit:
SELECT
*
FROM
table t
WHERE
MONTH(t.date) = 9 and
DAY(t.date) BETWEEN 1 and 30
It's also easier to understand.
Related
How can i select the rows with all dates in a given week number?
I have rows with dates like: 2019-01-05, 2019-01-08 etc
Now i would like to select all these dates thats in week 1, week 2 or week 5.
SELECT * FROM mytable
WHERE myDateColumn = WEEK(5)
Is this possible?
WHERE week(myDateColumn) in (1,2,5)
I found no other solution than use mySQL BETWEEN.
First find start and end date for the week number:
$dto = new DateTime();
$week_start = $dto->setISODate($chosenYear, $chosenWeek)->format('Y-m-d');
$week_end = $dto->modify('+6 days')->format('Y-m-d');
Then use BETWEEN
SELECT * FROM myTable WHERE date BETWEEN '$week_start' AND '$week_end'
I want to take all the records from a specific year.
My table is race_details and the year is a part of the date in race_date (in unix timestamp)
SELECT *
FROM race_details AS r
WHERE r.race_date=(SELECT MAX(YEAR(FROM_UNIXTIME(race_date)))
FROM race_details)
Unless there's more to the problem, I think you're overcomplicating your query:
SELECT *
FROM race_details AS r
WHERE YEAR(race_date) = 2014;
Substitute 2014 for the year to look up, and if it's a variable, remember to parameterize!
You need to compare the year of the race_date, not the whole race_date:
WHERE YEAR(race_date) = (SELECT ...)
I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.
I want to know if a date (Year-Month-Day) is between a day of the month (Month-Day).
For example, I want to know if 'April 2, 2013' is between 'April 2' and 'April 19'.
I can easily do this of the range has a year value. Howver, without a year, I need ideas how to do this.
Some examples of what I'm trying to achieve:
SELECT 1 WHERE '2013-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-03-02' BETWEEN '04-01' AND '04-19'; -- result is 1
Thanks
You can make use of DATE_FORMAT().
Example
SELECT
*
FROM
table
WHERE
DATE_FORMAT(date,'%m-%d') BETWEEN '04-01' AND '04-19'
Edit
SELECT
*
FROM
table
WHERE
STR_TO_DATE(date, '%m-%d') BETWEEN '04-01' AND '04-19'
Following is work with oracle
select * from table_name where to_char(date_column, 'MM-DD') BETWEEN '08-01' AND '08-14'
The accepted answer is good, but it does not work if you want to select from December to February as #tony-brix said. You can extend that answer with two DATE_FORMAT functions. One goes from December to end of year, other goes from start of year to February.
SELECT
*
FROM
table
WHERE
DATE_FORMAT(date,'%m-%d') BETWEEN '12-01' AND '12-31'
OR
DATE_FORMAT(date,'%m-%d') BETWEEN '01-01' AND '02-01'
I mentioned database table structure below,
name, expired_date, address, payment_date
----------------------
name1, 2013/06/02, address1,2013/07/23
name2, 2013/06/02, address2,2013/07/23
name3, 2013/04/02, address3,2013/07/23
name4, 2013/05/02, address4,2013/07/23
name5, 2013/06/02, address5,2013/07/24
...
name6, 2013/06/02, address6,.....
In this table I update date in yyyy/mm/dd format . current month is April but I need after two records only.
For example I need 06 month records only.
I need SQL query for this.
Assuming you want to query by expired_date:
Following query gets records after two months from current date:
SELECT * FROM tableName where expired_date>=DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
If you specifically want records for June:
SELECT * FROM tableName where expired_date>='2013-06-01' AND expired_date<'2013-07-01'
Try it.
Select [fields] from [yourtable] where Month([yourDateField]) = [Value, ex: for June : 6]
Implement
I am assuming you want to check expired_date
Select * from Table1 where Month(expired_date) = 6
if you want to check year also then, assuming year is 2013.
Select * from Table1 where Month(expired_date) = 6 and Year(expired_date) = 2013