Given a Table with from_date date and a to_date date column, I want to select all sets of data where an year, for example 2007, is the year of one of the dates or both or is in between the two dates.
How can i achieve this using mySQL?
Table:
create Table Articleprice
(`ArtikelpreisNR` int, `ArtikelNR` int, `from_Date` date, `to_date` date, `price` int);
Example Testdata
insert into Articleprice
(`ArtikelPreisNR`, `ArtikelNR`,`from_Date`, `to_Date` ,`price`)
values (1, 1, '2006-7-04', '2008-7-04', 10);
SQL:
This is returning only the ones, when from_date and to_date = 2007
SELECT
MIN(price)
FROM Articleprice
WHERE Articleprice.from_Date >= '2007/01/01' and Articleprice.to_Date <= '2007/12/31';
What do I need to do in order to solve my Case?
First you build complete dates using the year. For 2007, the dates would be 2007-01-01 and 2007-12-31 (assuming that end dates are inclusive).
Next you use overlapping dates query to find all rows that (i) end inside (ii) start inside (iii) contain or (iv) contained within the year 2007. The query is simply:
SELECT * FROM Articleprice
WHERE '2007-12-31' >= from_Date AND to_date > '2007-01-01'
Assuming your dates are actually dates and not strings, you need to use the proper date format in your query: YYYY-MM-DD not YYY/MM/DD:
WHERE Articleprice.from_Date >= '2007-01-01' and Articleprice.to_Date <= '2007-12-31';
Related
I have a table that links an employee to a project, the columns are:
BIGINT ID;
BIGINT FK_PROJECT;
BIGINT FK_EMPLOYEE;
DATE FROM;
DATE TO;
from and to are the first and last day during which the employee will be assigned to the project,
now what I would like to do is getting records by using the id of the employee, the year and month, currently I have a whole date to give to the query but I can extract month and year if necessary. I need a query that by using these infos it fetches the records in which the year and month provided are included in the time period from-to, like if I have employee 3 and march 2022 I get all the projects said employee was assigned to in march 2022.
A project can last more than a year.
An employee could be assigned from 23/05/2022 to 29/08/2022.
I already tried select where fk_employee = 3 and 2022-03-01 between from and to but it clearly wouldn't work if from and to were in random days of the month like 13 and 23
Can you guys help me? I'm not good at all in sql
You can use EXTRACT function
Try:
SELECT ID,
FK_PROJECT,
FK_Employee
FROM tablename
WHERE EXTRACT(YEAR_MONTH FROM 'your_date') BETWEEN EXTRACT(YEAR_MONTH FROM `from`) and EXTRACT(YEAR_MONTH FROM `to`)
Fiddle
SELECT ID,
FK_PROJECT,
FK_Employee
FROM tablename
WHERE DATE > '2020-01-01'
AND DATE < '2022-01-01';
Not that if this does not give you the results you are expecting, the most likely reason is that the DATE field is not defined as a DATE OR DATETIME column in MySql. If this is the case, you can change the column type, or if that is not an option, you can use str_to_date to treat that column as a date column for this query.
I'm working on a project for a Fuel Pump. Here is my table for the records:
I want to display rows from very start (2019-07-03) till the end of the specific month (e.g 2019-09-29) . How could i do achieve this?
A simple WHERE clause will do the trick
SELECT id, date, total_amount, total_paid
FROM table
WHERE date <= LAST_DAY(CURDATE())
CURDATE() will return current date i.e. 2019-09-08
LAST_DAY() will return the last date of current month i.e. 2019-09-30
WHERE clause will return all rows with date <= 2019-09-30
Update
If you want to filter records based on user input which is month and year ( 2019-09 ) then either it can done by appending '-01' at scripting side or using CONCAT at MySQL level,
WHERE date <= LAST_DAY(CONCAT('2019-09', '-01'))
I think this will work. You can change dates accordingly.
Select *
From table
Where date>='2019-07-03' AND date<='2019-09-29'
In mysql database I have table like this:
StartDate EndDate Price
01.01.2017 01.06.2017 100
02.06.2017 01.12.2017 150
What is the best solution to get price between some days. For example if I send dates 25.05.2017 as start date and 05.06.2017 as end date I need to get sum of price between these dates.
Try this
select sum(Price) as total_price from table_name
where StartDate >= 'given_startdate' and EndDate <= 'given_enddate'
To find overlapping periods use following logic:
where start_1 <= end_2 and end_1 >= start_2
in your case:
where startdate <= date '2017-06-05' and enddate >= date '2017-05-25'
Edit:
Seems like you want to expand the date range to one row per day. Join to a calendar table (if you don't have one, yer, create it, you will used it in many places):
select sum(price)
from mytable join calendar
on calendardate between startdate and enddate
In my sales table have the date column and the date is formatted like this January 9, 2018, 5:06 pm. How can I specifically get all the sales made from the month of January 2018 using MySQL?
assuming you date column is a date data type columns you can use year() and month() function
select * from my_table
where year(my_date_col) = 2018
and month(my_date_col) = 1
Since
the date is formatted like this January 9, 2018, 5:06 pm
and this is not recognizable time format for MySQL I belive the solution is:
SELECT * FROM table WHERE date like 'January%2018%';
I know this is not a "best practice" as well as storing time in a wrong type.
Since the column isn't already in a DATE format, let's assume it has to stay in its current format for the application purposes and add an auxiliary DATE column to select on (because SELECTs will be more optimized this way).
First we add a VIRTUAL column (requires MySQL 5.7+), which is a calculated column that is calculated at runtime but not stored on disk. We don't need to store this column on disk because we're going to index it later. Our VIRTUAL column will convert the application date's format to a MySQL DATE.
Replace str_date with your current date column:
ALTER TABLE `my_table`
ADD `virtual_date` DATETIME AS (STR_TO_DATE(`str_date`, '%M %d, %Y, %l:%i %p')) VIRTUAL AFTER `date`;
Now add the INDEX:
ALTER TABLE my_table ADD INDEX (`virtual_date`);
Now we can perform a properly indexed SELECT:
SELECT *
FROM my_table
WHERE virtual_date >= '2018-01-01'
AND virtual_date <= '2018-01-31'
or
SELECT *
FROM my_table
WHERE virtual_date >= '2018-01-01'
AND virtual_date <= LAST_DAY('2018-01-01')
The MySQL DATE format is YYYY-MM-DD. The latter query is logically simpler; the LAST_DAY() function will give you the last DATE of the month given a DATE or DATETIME value.
I have the following data in the table
from_date to_date
2015-05-12 2015-10-20
2015-10-21 2016-02-02
2016-02-03 NULL
Where NULL value in to_date denotes that this record is valid until this time.
The records I want to retrieve is between '2015-10-30' and '2016-08-08'. How do I get the second and third rows based on my search criteria?
I am confused why are you expecting the second row in the result set. Is it something loose range searching (either by from_date or by to_date)?
You can try something like that:
SELECT
*
FROM your_table
WHERE from_date >= startDate
AND IF(to_date IS NULL, TRUE, to_date <= endDate);
Note: Here startDate and endDate are the dates in your given range.
EDIT:
SELECT
*
FROM your_table
WHERE
'2015-10-30' BETWEEN from_date AND COALESCE(to_date,CURDATE())
OR
'2016-08-08' BETWEEN from_date AND COALESCE(to_date,CURDATE())
Finally, I found out what I was looking for.
select * from table where
end_date >= start_search && start_date <= end_search
More detailed answer in the link below. Answered by pacerier
mysql-query-for-certain-date-range