Getting data between two date strings in MySQL - 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!

Related

MySQL select multiple date range

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'

How to convert string with UTC timediff to mysql datetime

I am having application that fetches data from RSS Feed. Feed item contains pubDate which is of format Mon, 26 Feb 2018 03:00:00 -0800 like this. I need to save it in mysql database. I am trying to convert the string to datetime using str_to_date function. I don't know what exactly -0800 is. Here is my code
select str_to_date('Mon, 26 Feb 2018 03:00:00 -0800', '%W, %e %b %Y %H:%i:%s %T') ;
Its is giving me null, as the last value is not converted. What exactly i have to update with %T
Just ignore the -0800 and run you get the required result.output after removing the -0800

Area Booking System By Date Query For Maximum Area Availability within Date Range

Im trying to implement a simple area booking application by date
I record the booking requests
Total Area is constant: Assume 100sqft
Table:
req_made
startdate|enddate|area|storagename
Example:
01 Aug|__________________________________100_avail________________________|31 Aug
06 Aug|____________________________50_____________|25 Aug
05 Aug|____40__________|11 Aug
18 Aug|____30_______|23 Aug
13 Aug|_10_|16 Aug
My Request: 08 Aug|#######################|20 Aug
Answer=>Maximum possible within range 10
Area Free:
|-------100---||---10----------|-|-40-|50|-------20----|-50|--100--|
60 50
Same Diagram in written format (Picture)
My SQL Query Attempt:
SELECT (100-SUM(req_made.area)) AS area,req_made.storagename
FROM req_made
WHERE (req_made.enddate >= '2016-08-20' AND
req_made.startdate <= '2016-08-20')
OR(req_made.enddate>='2016-08-08' AND
req_made.startdate<= '2016-08-08')
OR(req_made.enddate<='2016-08-20' AND
req_made.startdate>= '2016-08-08')
GROUP BY req_made.storagename
Result
-30|my_storage
Expected
10|my_storage

Order one field both ASC and DESC

I've got a table with events that I'd like to show. I want to sort my events in the future ascending, and the past in descending order. How can I do this with one query?
My current order
Dec 23
Nov 10
Nov 04
Sep 12
Aug 01
Jul 23
The order I want
Nov 4 ** changed place
Nov 10
Dec 23 ** changed place
Sep 12
Aug 01
Jul 23
Many thanks in advance.
You are asking to sort by the distance from current date:
SELECT *
FROM table
ORDER BY IF(date_field >= NOW()),0,1), ABS(DATEDIFF(NOW(), date_field))

Convert a date string into proper date stamp in MYSQL (I need the sql code, not php)

I have a huge spreadsheet with four date columns for the details of various orders that I'm importing from one system to another.
The dates currently look like this in the MYSQL database I created from the spreadsheet:
Sun Nov 27 17:02:40 PST 2011
Sat Nov 26 17:01:54 PST 2011
Mon Nov 28 18:06:29 PST 2011
and I want them to be simply a date stamp that I can query for running reports against in MYSQL. I'm not using PHP right now and I want to be able to do this conversion in MySQL. I've found similar threads, but nothing that was helpful for this particular approach. Apologies if it's been addressed somewhere already.
Use str_to_date
select str_to_date('Sun Nov 27 17:02:40 PST 2011', '%a %b %e %H:%i:%s PST %y')
I don't think you can convert the timezone name properly though.