MySQL select multiple date range - mysql

I have a table with 2 dates: startDate and EndDate
I pass to my query 2 dates and I want to check if in this date range the offer are available.
I have an offer that is valid From 1 may 2019 to 30 may 2019.
When I search in offers, I want to show this only if the date range that I pass is included in the date range of the offer.
OFFER From 1 may 2019 to 30 may 2019
Value pass: 01 jan 2019 to 4 jan 2019 NO SHOW
Value pass: 28 april 2019 to 2 may 2019 NO SHOW
Value pass: 10 may 2019 to 12 may 2019 ONLY SHOW IN THIS CASE
Value pass: 29 may 2019 to 2 june 2019 NO SHOW
Value pass: 10 nov 2019 to 12 now 2019 NO SHOW
How can I do this?

This will work for MySQL.
date_from < '2019-10-05' AND date_to > '2019-10-12'

Related

Mysql Order By month days

01-09-2016 01,September 2016 12
02-09-2016 02,September 2016 21
03-09-2016 03,September 2016 19
04-09-2016 04,September 2016 15
05-09-2016 05,September 2016 19
23-08-2016 23,August 2016 21
24-08-2016 24,August 2016 22
25-08-2016 25,August 2016 24
26-08-2016 26,August 2016 24
27-08-2016 27,August 2016 18
I want result like below
23-08-2016 23,August 2016 21
24-08-2016 24,August 2016 22
25-08-2016 25,August 2016 24
26-08-2016 26,August 2016 24
27-08-2016 27,August 2016 18
01-09-2016 01,September 2016 12
02-09-2016 02,September 2016 21
03-09-2016 03,September 2016 19
04-09-2016 04,September 2016 15
05-09-2016 05,September 2016 19
You should change the date format. Period.
The proper date format your database supports is chosen not by a whim but for the very reason of making the proper sorting and comparison possible.
Therefore, change your values format to 2016-09-01 and the field type to date. And you will never have not a single problem neither with sorting nor comparison nor any date conversions.

SSRS Report with Business days as Expression

I have an SSRS report with the following header,
Date 28-Mar 27-Mar 25-Mar 24-Mar 23-Mar 22-Mar 21-Mar 20-Mar
Day Fri Thu Wed Tue Mon Sun Sat Fri
Pending 1 2 3 4 5 5 5 6
I able to generate Date and Day rows using expression with current date.
(e.g. = Left(WeekDayName(WeekDay(DateAdd("d",-1,Now()))),3))
But Pending Days row should display age in days with same age number for Mon, Sun and Sat since Sun and Sat are off days.
Is it possible to generate with expression?
Got it Guys,
We just need to make sum of no of 'mon', 'tus'..'fir' in the date range.
=DATEDIFF("ww",DateAdd("d",-1,Now()),Now(), vbMonday)
+
DATEDIFF("ww",DateAdd("d",-1,Now()),Now(), vbTuesday)
etc.
:)

Sql Server Query not getting desire Result

I want show record month wise and date wise. My query is this but I am not getting how I can achieve this.
Query is
select
iWorkItemId
, convert(varchar(20),Progress.dtProgressDate,106) as dtProgressDate
, vsDescription
, WPD.rWeightage
, Progress.ProgressPerc as ProgressPerc
, Convert(decimal(6,2)
, ((WPD.rWeightage * Progress.ProgressPerc)/100)) as TotalProgress
from tblPkgWorkItems WPD
join tblPkgWorkItemsProgress Progress on WPD.iWorkItemId = Progress.iWorkItemsId
where iPackage='7'
group by iWorkItemId, dtProgressDate,vsDescription,rWeightage,ProgressPerc,progressmonth
order by progressmonth ASC,dtProgressDate ASC , iWorkItemId ASC
Column name : (1) progressmonth (Store Month Name)
(2) dtProgressDate (Store complete date)
Example :
In our table we are storing the records like in this manner:
30 Apr 2013
31 Aug 2013
28 Feb 2013
30 Jan 2013
Then I want to show records like this
30 Jan 2014
if some record is inserted in 15 jan 2014 then After 30 jan 2014 then 15 jan 2014 record will show.
28 Feb 2014
30 Apr 2014
30 Aug 2014
Means firstly Jan records will come then Feb record then March records.
Means all records will come month by order then sort by date wise .
My data is being returned like this:
iWorkItemId dtProgressDate progressmonth rWeightage
320 30 Apr 2014 Apr 2
321 20 Apr 2014 Apr 3
320 10 Feb 2014 Feb 4
321 9 Jan 2014 Jan 7
I want record should come first 9 jan 2014 then 10 Feb 2014 (Means Order by Month) then after it 30 April 2014, then 20 april 2014 (Then Sot by Date).
iWorkItemId will repeat win every month.
For my problem is I want to show
Firstly Jan records then Feb records , then March records
And whenever Jan month is coming then All Date which is belong to jan month that will come under JAN month.
Firstly Higher date of JAN month (Means ) 31 Jan 2014, 30 jan 2014, 29 jan 2014, 28 feb 2014, then 27 feb 2014.
Means first records fetch month wise then on particular month that records sort by datewise

Getting data between two date strings in MySQL

I have been recording Twitter data for a project I'm working on the date information is saved as Thu, 14 Jul 2011 06:21:48 +0000 in a string field.
How do I select data that falls between two dated using mySQL? I can get data larger than a value or smaller than a value but not between to values.
The data for example is:
Thu, 14 Jul 2011 06:21:48 +0000
Thu, 14 Jul 2011 12:18:21 +0000
Thu, 14 Jul 2011 18:48:00 +0000
Thu, 14 Jul 2011 23:48:02 +0000
Fri, 15 Jul 2011 06:48:10 +0000
Fri, 15 Jul 2011 12:48:00 +0000
Fri, 15 Jul 2011 18:43:32 +0000
Fri, 15 Jul 2011 23:44:08 +0000
Sat, 16 Jul 2011 06:47:08 +0000
Sat, 16 Jul 2011 12:46:49 +0000
Sat, 16 Jul 2011 18:45:41 +0000
Sat, 16 Jul 2011 23:41:27 +0000
My SQL string is:
SELECT *
FROM twitter
WHERE SUBSTR(twitter_date, 6, 11) >= '2011-06-15'
AND SUBSTR(twitter_date, 6, 11) <= '2011-06-21'
I've tried BETWEEN statements as well but no luck.
Any help will be appreciated!
You can't use between because they are strings and not actual date fields. If you know the format of the date will always be the same, you can do the following:
STR_TO_DATE(str,format)
SELECT *
FROM twitter
WHERE STR_TO_DATE(twitter_date, '%a, %c %b %Y %k:%i:%s')
between '2011-06-15' AND '2011-06-21'
You are comparing "6 Jul 2011" with 2011-06-15.
It should be
SELECT *
FROM twitter
WHERE SUBSTR(twitter_date, 6, 11) >= '6 Jul 2011'
AND SUBSTR(twitter_date, 6, 11) <= '21 Jul 2011'
You may want to look into MySQLs STR_TO_DATE function. This will give you a date and between should work as expected.
The query will fail. You're comparing two STRINGS, not dates. You need to tell MySQL explicitly that you want to treat the strings as dates. Since it's date information, store it in a date or datetime field type in MySQL, which saves you all this trouble:
SELECT STR_TO_DATE(SUBSTR(twitter_date, 6, 11)) >= '2011-06-15' ...
Forcing your left-hand-side to be a proper date value will hint to MySQL that it should treat the right-hand-side as a date as well.
Once you have altered your table to hold the date data in proper DATETIME fields, you may want to refer to my answer to the following question regarding dates and filtering them, which may prove useful.
For example, in your query / data above, what would happen if you had no data for one of the dates you were querying? You'd get no output for that date, which may look odd when charting, etc. If you use the technique I describe in the attached question, you can get that additional information, and other useful stuff like the day of the week, month name, etc.
Select all months within given date span, including the ones with 0 values
Hope that helps!

MySql Query- Date Range within a Date Range

I use mySql 5 and IIS.
I have products, that have a start date field and an end date field.
I need to run a query that will take user entered Start and End dates, and output the number of days that the product ran within the date range.
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru Sept 15 2011
Results = 31
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru July 15 2011
Results = 15
If your products have a start_date and an end_date and your query has a qstart_date and a qend_date, then we want the number of days between:
GREATEST(start_date, qstart_date)
and
LEAST(end_date,qend_date)
. In MySQL I think this looks like
1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) )
And you'll want to ignore negative numbers, replacing them with "0".