Date
9/25/2015
9/26/2015
9/27/2015
9/28/2015
9/29/2015
9/30/2015
10/1/2015
10/2/2015
10/3/2015
10/4/2015
10/5/2015
Can anyone help me in MySQL. I would like to select only date from 9/28/2015 to 10/4/2015.
Please take note, this date is in Text field.
Thank you.
you can use STR_TO_DATE(yourdatefield, '%m/%d/%Y') to convert text to date and you can later use between clause to restrict output data.
Convert first your dates using CONVERT, then use BETWEEN in your WHERE clause.
Try this..
SELECT * FROM TableName
WHERE Date BETWEEN CONVERT(DATE,'9/28/2015') AND CONVERT(DATE,'10/4/2015')
You can try like this:
WHERE `Date` BETWEEN CAST('9/28/2015' AS DATE) AND CAST('10/4/2015' AS DATE)
or
WHERE STR_TO_DATE(`Date`, '%m/%d/%Y') BETWEEN STR_TO_DATE('9/28/2015', '%m/%d/%Y') AND STR_TO_DATE('10/4/2015', '%m/%d/%Y')
DEMO
Also try to avoid storing dates as Text. Instead use Date datatype to store dates.
Try this , in where clause used function str_to_date
SELECT `dateVal`,`id`
FROM `datecheck`
WHERE STR_TO_DATE(`dateVal`,'%m/%d/%Y') between STR_TO_DATE('9/28/2015',
'%m/%d/%Y') AND STR_TO_DATE('10/4/2015', '%m/%d/%Y')
I had the same type of issue but in my case the date stored also contains the time (also in a text field, for instance 2017-09-11 05:07:58 PM). This is for a wordpress site but I want to query the database directly, the date is in a meta_value.
To make this work I ended using a subbstring of the date, i am posting this in case it helps someone:
SELECT ID, display_name, user_email, meta_value FROM bfge_users, bfge_usermeta WHERE (bfge_users.ID = bfge_usermeta.user_id) AND meta_key ='user_login' AND CAST(SUBSTR(meta_value,1,POSITION(' ' IN meta_value)) AS DATE) between '2017-09-11' AND '2017-09-13';
use between, you can read more here:
Select data from date range between two dates
and question is dublicate
Related
I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())
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.
I have a table. It has a varchar type date column.
I want to search that table using date, but i want to search only from month with year.
example: 12-2016
How can I write the query for that?
I want that part to assign to a variable of jasper
**example:**WHERE ............. =$P{invDate}
example as in my jasper report:
SELECT invoiceheader.inNo AS invoiceheader_inNo, invoiceheader.cusID AS invoiceheader_cusID, invoiceheader.soNo AS invoiceheader_soNo, invoiceheader.total AS invoiceheader_total, invoiceheader.dis AS invoiceheader_dis, invoiceheader.netTotal AS invoiceheader_netTotal, invoiceheader.cash AS invoiceheader_cash, invoiceheader.bal AS invoiceheader_bal, invoiceheader.date AS invoiceheader_date FROM invoiceheader invoiceheader WHERE ...........................................=$P{invDate}
I need a perfect query for the above space
Try this (you have used varchar data type for date so like keyword will perfectly suitable for this situation):
select * from table where date like concat('%' ,$P{invDate});
Try this case:
STR_TO_DATE(date_field, '%d-%m-%Y') < STR_TO_DATE('12-2016', '%m-%Y')
Try following -
select * from Table_name where month(date_column_name)=12 and year(date_column_name)=2016
I have this problem if anyone can help.
There is a field (date) in my table (table1) that is a date in the format 3/31/1988 (M/D/y), and my necessity is to define how many days have passed since that date.
I have tried to give this instruction
SELECT DATEDIFF(CURDATE(), date) AS days
FROM table1
But it gives back 'null' and I think this happens because the two date formats are different (CURDATE() is YMD.....
Is it correct? can anyone help me?
Thank you in advance
You can use STR_TO_DATE():
SELECT DATEDIFF(CURDATE(),STR_TO_DATE(date, '%m/%d/%Y')) AS days
FROM table1
SQLFiddle Demo
Your DATE field should have DATE or DATETIME format to be used as DATEDIFF argument correctly.
Also DATE is MySQL keyword and I am not sure that you can use it as valid field name.
You can use this for accurate result
SELECT DATEDIFF(CURDATE(), DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`date`)), '%Y-%m-%d')) AS days FROM `table1`
If you want to consider results without - signs that you have to follow parameters position as below :
SELECT DATEDIFF(Big_Date,Small_Date) AS days FROM table1.
positive results e.g 5 (with no sign), if you place a Small date as the first parameter then it will results minus sign e.g -5.
Hi I was wondering why this statement is working in mySQL
SELECT COUNT(*) AS `numrows`
FROM (`myTable`)
WHERE DATE_FORMAT(creationDateTime, '%m/%d/%Y') BETWEEN '02/21/2011' AND '03/20/2011'
but this is not
SELECT COUNT(*) AS `numrows`
FROM (`myTable`)
WHERE DATE_FORMAT(creationDateTime, '%m/%d/%Y') BETWEEN '12/21/2010' AND '03/20/2011'
The first statement returns 'xx' count of the number of rows while the second one returns '0'
The only difference I see is that the "from" date is in 2010 and the "end" date is in 2011. To test if this was the problem I queried from '12/31/2010' and it still gave me 0 results but when I set the start date as '01/01/2011' it gave me the number of records that were created in that time span.
Is there something I am missing with regards to mySQL's BETWEEN and using dates from different years?
Thanks for the help!
Try using the date format BETWEEN '2010-12-21' AND '2011-03-20'. Also remove the DATE_FORMAT() function.
So, this:
SELECT COUNT(*) AS `numrows`
FROM `myTable`
WHERE creationDateTime BETWEEN '2010-12-21' AND '2011-03-20'
DATE_FORMAT() returns a string, not a date. By using it you're forcing a string comparison instead of a date comparison. You should omit the DATE_FORMAT and use YYYY-MM-DD date strings instead.