sql select from table with date range condition

sql query date range from current date
sql query between two dates and times
sql between date range
sql query between two dates column
select data between two dates in mysql
sql query date greater than
sql between two dates inclusive
sql where date

I have this table

CREATE TABLE `tarif` (                  
  `tarif_id` int(11) NOT NULL AUTO_INCREMENT,
  `start_tarif` date NOT NULL,
  `end_tarif` date NOT NULL,
  `day_tarif` varchar(50) NOT NULL,
  PRIMARY KEY (`tarif_id`)

INSERT INTO `tarif` VALUES (1, '2019-02-01', '2019-02-10', '10'),
                           (2, '2019-02-11', '2019-02-20', '20'),
                           (3, '2019-02-21', '2019-02-28', '10'),
                           (4, '2019-03-01', '2019-02-10', '15');

How can I get day_tarif between 2019-02-05 and 2019-02-15 and calculate the sum = day_tarif * number of date betwwen 2019-02-05 and 2019-02-15

You can use the following solution using DATEDIFF:

SELECT SUM((DATEDIFF(LEAST(`end_tarif`, '2019-02-15'), GREATEST(`start_tarif`, '2019-02-05')) + 1) * `day_tarif`) AS sumCustom
FROM `tarif` 
WHERE `end_tarif` >= '2019-02-05' AND `start_tarif` <= '2019-02-15'

demo on dbfiddle.uk

You can use the following solution using SUM and COUNT:

SELECT SUM(`day_tarif`) * COUNT(`tarif_id`) 
FROM `tarif`
WHERE `end_tarif` >= '2019-02-05' AND `start_tarif` <= '2019-02-15'

SQL BETWEEN Operator, The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included. BETWEEN Syntax. SELECT column_name(s​) The SQL BETWEEN Operator The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

select SUM(day_tarif) * COUNT(tarif_id) 
from tarif
where end_date >= '2019-02-05' AND start_date <= '2019-02-15'

This question has been asked several times, or something like it. Be sure to search StackOverflow before asking a duplicate: SQL query to select dates between two dates

Selecting records between two date range query, Query for Getting records between two years, month or date ranges from a hi i need the query for display the dates between the two dates, and my condition is this SQL code better. cos ikeep getting an error: SELECT qryViewExcesses. Between two date ranges Now let us move to select a range of records between two dates. Here is the SQL for this SELECT * FROM `dt_tb` WHERE dt BETWEEN '2005-01-01' AND '2005-12-31' Date Format to use in query You have seen we have used 'Y-m-d' date format in our query. We may not get data always in this format so here is the PHP code to convert to this format before using inside a query.

You can try following code:

 WITH cte AS(
 FROM tablename
 WHERE end_tarif >= '2019-02-05' AND start_tarif <= '2019-02-15'
 SELECT day_tarif, day_tarif * COUNT(tarif_id) AS 'SUM'
 FROM cte
 group by day_tarif;

SQL WHERE BETWEEN and WHERE NOT BETWEEN, Dates and , WHERE BETWEEN returns values that fall within a given range. SELECT column-names; FROM table-name; WHERE column-name BETWEEN value1 AND  SQL BETWEEN dates example To find all employees who joined the company between January 1, 1999, and December 31, 2000, you check whether the hire date is within the range: SELECT employee_id, first_name, last_name, hire_date FROM employees WHERE hire_date BETWEEN '1999-01-01' AND '2000-12-31' ORDER BY hire_date; See it in action

I think this might be what you are after...

Declare @tarif as table (                  
  tarif_id int NOT NULL ,
  start_tarif date NOT NULL,
  end_tarif date NOT NULL,
  day_tarif varchar(50) NOT NULL

INSERT INTO @tarif VALUES (1, '2019-02-01', '2019-02-10', '10'),
                           (2, '2019-02-11', '2019-02-20', '20'),
                           (3, '2019-02-21', '2019-02-28', '10'),
                           (4, '2019-03-01', '2019-02-10', '15');

-- Declare parameters
Declare @paramstart date, @paramend date
Set @paramstart='2019-02-05'
Set @paramend='2019-02-15'

-- Set up loop
Declare @mincount int, @maxcount int, @myval int, @curstart date, @curend date,@curtarif int, @mytarif int

Set @mincount=(Select MIN(tarif_id) from @tarif where  end_tarif >= '2019-02-05' AND start_tarif <= '2019-02-15')
Set @maxcount=(Select Max(tarif_id) from @tarif where  end_tarif >= '2019-02-05' AND start_tarif <= '2019-02-15')
Set @mytarif=0

-- Do loop
WHile @mincount<=@maxcount

Set @curstart=(Select start_tarif from @tarif where  tarif_id=@mincount)
Set @curend=(Select end_tarif from @tarif where  tarif_id=@mincount)
Set @curtarif=(Select cast(day_tarif as int) from @tarif where  tarif_id=@mincount)

IF @paramstart between @curstart and @curend 
    Set @mytarif=@mytarif+((DATEDIFF(day,@paramstart,@curend)+1) * @curtarif)

IF @paramend between @curstart and @curend 
    Set @mytarif=@mytarif+((DATEDIFF(day,@curstart,@paramend)+1) * @curtarif)

IF @paramstart not between @curstart and @curend  and @paramend not between @curstart and @curend
    Set @mytarif=@mytarif+((DATEDIFF(day,@curstart,@curend)+1) * @curtarif)

Set @mincount=@mincount+1

Select @mytarif as tarif

MySQL: BETWEEN Condition, The MySQL BETWEEN condition is used to retrieve values within a range in a SELECT, INSERT, UPDATE, or DELETE statement. Alter Table · AUTO_INCREMENT · Change Password · Comments in SQL · Create Table The following date example uses the BETWEEN condition to retrieve values within a date range. Introduction to SQL SELECT statement. To query data from a table, you use the SQL SELECT statement. The SELECT statement contains the syntax for selecting columns, selecting rows, grouping data, joining tables, and performing simple calculations. The SELECT statement is one of the most complex commands in SQL, therefore, in this tutorial, we

Try this!

select day_tarif , sum(day_tarif * count (*)) as sum
where start_tarif between '2019/02/05' and '2019/02/15' and end_tarif between '2019/02/05' and '2019/02/15'
group by day_tarif;

SQL Server: BETWEEN Condition, This SQL Server tutorial explains how to use the BETWEEN condition in SQL values within a range in a SELECT, INSERT, UPDATE, or DELETE statement. example uses the BETWEEN condition to retrieve values within a date range. The SQL SELECT Statement. The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

How to Use SQL BETWEEN Correctly for Date Range Searches , Are you using the BETWEEN operator to filter using a date range? Click to learn Your SQL questions, answered SELECT SalesOrderID, SalesOrderDate; FROM SalesOrder; WHERE Thus, the condition in our query will be equivalent to: select A.* from table_A A inner join table_B B on A.id = B.id where B.tag = 'chair' That WHERE clause I would like to be: WHERE B.tag = A.<col_name> or, in my specific case: WHERE B.val BETWEEN A.val1 AND A.val2 More detailed: Table A carries status information of a fleet of equipment.

SQL Server BETWEEN Operator By Practical Examples, The condition that uses the BETWEEN operator is much more readable the one See the following products table from the sample database: To get the products whose list prices are in the range 149.99 and 199.99, you use the NOT BETWEEN operator as follows: B) Using SQL Server BETWEEN with dates example. delete from YOUR_TABLE where your_date_column < '2009-01-01'; This will delete rows from YOUR_TABLE where the date in your_date_column is older than January 1st, 2009. i.e. a date with 2008-12-31 would be deleted.

PostgreSQL BETWEEN, You use the BETWEEN operator to match a value against a range of values. You often use the BETWEEN operator in the WHERE clause of a SELECT, INSERT, UPDATE or DELETE statement. If you want to check a value against of date ranges, you should use the literal date in Conditional Expressions & Operators. To get data of 'agent_code' and maximum 'ord_date' with an user defined column alias 'Max Date' for each agent from the orders table with the following condition - 1. 'agent_code' should come in a group the following SQL statement can be used : SELECT agent_code, MAX(ord_date) AS "Max Date" FROM orders GROUP BY agent_code;

  • It's not a free coding site. Kindly share your efforts first then ask for specific help from others.
  • @a_horse_with_no_name considering the backticks, this is probably MySQL or MariaDB
  • What have you already tried? Please share any code you already have written.
  • i work with mysql database,i try this SELECT * FROM tarif WHERE start_tarif<='2019-02-15' && end_tarif>='2019-02-05',it work, but i can't get the product of number of day between 2019-02-05 and 2019-02-15 and the value of day_tarif
  • @user3623368 - what is your expected result?
  • here you multiply number of day with the sum of tarif_day in the line 1 an line2,that means 11*(10+20)=330. Or we need (6*10)+(5*20)=160
  • @user3623368 - You're welcome! Next time try to describe your issue more detailed and with the expected value or result. Many people want to help but only can help (useful) if they unterstand the issue and the expected logic. - helpful for future questions
  • now i wont to get number of monday in this rangedate , i run this code but it give me result 0; select count(*) from tarif where weekday(end_tarif<= '2019-02-21'AND start_tarif>='2019-02-05') = 0;
  • @user3623368 you want to exclude mondays from days count (days count - count mondays)?
  • if i have new column contain monday_tarif different from other day_tarif, to calculate global tarif i must specify number of monday and multiply it with mondy_tarif,and the rest of day will be multiply with day_tarif
  • now you are missing the day_tarif itself in the select list.
  • thanks for your answer, but you have not understand what i want,day tarif is not a date, it's varchar, and i want get it between 2019-02-05 and 2019-02-15, the start date 2019-02-05 contained betwwen start_tarif and end_tarif in the first line,and the end date 2019-02-15 contained betwwen start_tarif and end_tarif in the second line .that is the problem that i won't solve.