Query to Select where timestamp between date1 and date 2 - mysql

I have MySQL table
id product p_image
1 G images\20131030164545.jpg
2 S images\20131230164545.jpg
3 V images\20140110164545.jpg
4 R images\20140320164545.jpg
5 K images\20140526164545.jpg
6 L images\20150110164545.jpg
7 SK images\20150120164545.jpg
Here I need to extract products from above table where p_image timestamp between two dates (for example I need to extract from 2013/12/01 to 2014/07/30 dates)
In this query I need to extract timestamp from this string 'images\20140526164545.jpg' and convert this to date format and select values between two dates.

Assuming the format of the string is fixed (which it looks to be) you can use thesubstrfunction to extract the timestamp and then cast it to a date and filter by it. Something like this should work:
select * from table1
where cast(substr(p_image FROM 7 FOR 14) as date)
between '2013/12/01' and '2014/07/30'
Sample SQL Fiddle
There might be more efficient ways to do this, but this should give you an idea to start with.
Edit: if the string can vary the something like left(right(p_image, 18), 14) should work.

The dates in p_image are in YYYYMMDD format, so you can compare them as strings. That is, there is no reason to convert the strings to a date data type.
Hence you can just do:
where substr(p_image, 8, 8) between '20131201' and '20140730'
If the position of the date is not fixed but always after the /, you can do:
where left(substring_index(p_image, '/', -1), 8) between '20131201' and '20140730'

Try this it will working :
select * from `datetest` where substr(p_image, 8, 8) between '20131201' and '20140730'
Screenshot of Phpmyadmin :

Related

MySQL - sort by date column which is stored as text in format MMYYYY

I'm trying to sort by a column called date which stores various dates in format MMYYYY (for example, 122020, 102019, etc.).
The SQL query that I have looks like this:
SELECT `date`, `invested` FROM `savings` WHERE `id` = 123 ORDER BY STR_TO_DATE(`date`, "%m%Y") ASC but this query does not sort the output correctly.
Any ideas on how to sort it properly?
Important note: Reclassifying/modifying the column's type is not an option at this point.
EDIT: My current SQL query, which is described above, sorts the dates like this: 102019, 102020, 112019, 112020. But my goal is to have it like this: 102019, 112019, 102020, 112020.
Thank you
Try separating the date string into two parts (year and month), cast each part as integer, and then ordering by that number:
SELECT
date_,
invested
FROM
savings
WHERE
id = 123
ORDER BY
CAST(SUBSTRING(date_, 3, 6) AS UNSIGNED),
CAST(SUBSTRING(date_, 1, 2) AS UNSIGNED)
;
This will do the job :) refer to this DB Fiddle for clarification.

mysql date comparision is not working unable to find the issue

i have a huge data with dates as string.
column name date1
datatype varchar
the stored data is in this format:14-Mar-2016 05:44:38pm
Now I have split only date from this string like this: 14-03-2016
By using this: DATE_FORMAT(STR_TO_DATE(gr.date1, '%d-%M-%Y'),'%d-%m-%Y')
Now I am trying to compare the date with this query:
SELECT * FROM
( SELECT date1,DATE_FORMAT(STR_TO_DATE(date1, '%d-%M-%Y'),'%d-%m-%Y') as dateFormatted
FROM `grabt` ) as mTbl WHERE mTbl.dateFormatted >= '19-01-2016'
AND mTbl.dateFormatted <= '25-01-2016'
but it is not working what could be the possible error.?
The timestamp string 14-Mar-2016 05:44:38pm can be converted to a datetime using the STR_TO_DATE() along with the format string %d-%b-%Y %r. We can then obtain only the date portion by wrapping that with DATE(). Have a look here for a demo to see that this works.
SELECT *
FROM
(
SELECT DATE(STR_TO_DATE(date1, '%d-%b-%Y %r')) AS dateFormatted
FROM grabt
) AS mTbl
WHERE mTbl.dateFormatted BETWEEN '2016-01-19' AND '2016-01-25'
As Gordon already pointed out, you should ideally be using date types not strings for your date calculations. And by the way, use a valid date string when comparing in your WHERE clause. YYYY-MM-DD is a valid format, e.g. 2016-01-19, but 19-01-2016 is not.
Learn to use the right types for columns. Perhaps you are stuck with someone else's really bad decision to store date/times as strings. Perhaps you cannot change that. But, within a query, use the right types!
SELECT mTbl.*,
LEFT(date1, 10) as FormattedDate -- Is this really necessary?
FROM (SELECT date1,
STR_TO_DATE(LEFT(date1, 10), '%d-%M-%Y') as thedate
FROM `grabt`
) mTbl
WHERE mTbl.thedate >= '2016-01-19' AND
mTbl.thedate <= '2016-01-25';
This will do the comparison as dates not as strings.

convert string with time to date only

I have a column which holds date values as varchar as in the format of
date_n date_s
2016-11-10::12:36:05 2016-NOV-10::12:36:05
2016-11-10 2016-NOV-10
Now all I need is to get date from both of columns but I am only able to get from column date_n along with timestamp and nothing from date_s The query I tried as
SELECT * FROM table where CAST(date_n AS DATE)=CURDATE();
which returns both element as result but when I change date_n to date_s returns empty and all I need output as date only no time stamp
So is there any way to sort this out
Hi try this logic
WHERE TRIM(UPPER(SUBSTRING_INDEX(date_s,':',1))) =
TRIM(CONCAT(YEAR(NOW()),'
-',UPPER(SUBSTRING((MONTHNAME(STR_TO_DATE(MONTH(NOW()),'%m'))),1,3)),'-',DAY(NOW())))
WHERE date_s LIKE CONCAT(YEAR(NOW()),'-',UPPER(SUBSTRING(MONTHNAME(NOW()), 1, 3)),SUBSTRING(CURRENT_DATE(), 8), '%');

Query database where month is equal to

I have a query where I need to select entries only where their month is equal to a certain month.
Each entry has the date saved in the ymd format (20140211).
I have tried the following, but it doesn't recognise the month.
AND (MONTH(matrix.col_id_3) = '1' OR MONTH(matrix.col_id_4) = '1')
Is this due to the date format? I am stuck with this format as its part of the CMS I use.
Any help would be fantastic.
if you can use the default date column type (why?) so you have to convert your column
i.e.
... MONTH(STR_TO_DATE(matrix.col_id_3,'%Y%m%d')) = 1 ...
probably your dates are stored as strings, so you could use the following
.... SUBSTRING(matrix.col_id_3, 5, 2) = '01' ...

SQL order by and SQL date

I have 2 doubts related to SQL query's
id name update
1 some 2013-05-03
2 som 2013-05-08
3 smee 2013-06-05
How can i list items on a particular month (I want all records,year and date will not be specified I just want to check the month)
How can I arrange name in alphabetic order and arrange it as groups of names such as (limiting number of records =10)
Array A = names starting with A
Array B = names starting with B
The easiest way, to fetch MONTH from a DATE or DATETIME type of fields is to use the MySQL's date-time function MONTH().
For your query, it shall be:
SELECT *
FROM tblName
WHERE MONTH( `update` ) = <month Number such as 5>
The second would need a more complex query. I'd rather use php to do the grouping better(as I've more control over that language).
You can simply use datatype of the field as DATE or you can store any date as unix timestamp and then convert it whenever you want to show it.
Example: 1363979714 (ISO 8601:2013-03-22T19:15:14Z)
If you want list items on a particular date, you can write your query like this:
Month:
Select * from tableName where update like '%-5-%'
day:
Select * from tableName where update like '%-16'
year:
Select * from tableName where update like '2013-%'