I have a column(expire_date(varchar)) with value stored like 2016-05-11 13:19:32. I want to compare this value with NOW(). I want to select the rows with the expiry_date greater than present time
My code:
select * from tbl_table where expiry_date > NOW()
But I don't know whether this is correct or not.
Try this:
select * from tbl_table where CAST(expiry_date AS DATETIME) > NOW()
If you want to compare DATETIME:
select * from tbl_table where str_to_date(expiry_date,"%Y-%m-%d %H:%i:%s") > NOW();
If you want to compare ONLY DATE:
select * from tbl_table where date(str_to_date(expiry_date,"%Y-%m-%d %H:%i:%s")) > curdate();
Note: Date should be stored as date or timestamp in database, instead of varchar DATE in MySQL.
Instead of now() use current date() like:
select * from tbl_table where expiry_date > current_date();
Related
This is my code that should work but... it doesnt:
SELECT * FROM myTable WHERE token = 'token#' AND expiration_date >= now();
the wierd thing is.. when i do "<=" it bring me dates greater than the actual time.
Maybe is something with the now() configuration time or something.
Comparing the expiration date with now() would only work that way if the data type of expiration_date is datetime else just use
SELECT * FROM myTable WHERE token =
'token#' AND expiration_date >= CURTIME();
if you are comparing with TIME.
or
SELECT * FROM myTable WHERE token =
'token#' AND expiration_date >= CURDATE();
if you are comparing with the DATE.
I think it's just best to set expiration_date as datetime and then compare with now()
I store standard date ( with this format: ('Y-m-d H:i:s') ) in mysql database, now i want to select records that match this standard date with current date, in other word i want to select the rows where standard_date field demonstrate today's date.
use DATE() to strip off time in the datetime column. CURDATE() returns the current date.
SELECT *
FROM tableName
WHERE DATE(standard_date) = CURDATE()
SQLFiddle Demo (DATE() vs without DATE())
Just use:
select * from mytable where date(standard_date) = curdate();
select * from tablename where date = CURDATE()
CURDATE() returns the current date.
If you use DATE type, use CURDATE() function -
SELECT * FROM table WHERE date_field = CURDATE()
If you use DATETIME type, use CURDATE() function and DATE() function to get date part from datetime value -
SELECT * FROM table WHERE DATE(date_time_field) = CURDATE()
I have date stored as updatedOn (values like '2012-11-12 12:38:43')
My queries is:
select * from mytable
where updatedOn >= '11/12/2012' AND updatedOn <= '11/12/2012'
My goal is to get all records inclusive given from & to dates
If you avoid <= and use < instead, you end up being forced to use logic that works regardless of whether or not your data has a time component.
SELECT
*
FROM
myTable
WHERE
updatedOn >= '2012-12-11'
AND updatedOn < '2012-12-11' + INTERVAL '1' DAY
You need to take the DATE() component of your updatedOn column and format your date literals in one of the formats supported by MySQL:
SELECT *
FROM mytable
WHERE DATE(updatedOn) BETWEEN '2012-12-11'
AND '2012-12-11'
Or else, use MySQL's STR_TO_DATE() function to convert your strings to MySQL dates:
SELECT *
FROM mytable
WHERE DATE(updatedOn) BETWEEN STR_TO_DATE('11/12/2012', '%d/%m/%Y')
AND STR_TO_DATE('11/12/2012', '%d/%m/%Y')
You need to try
select * from mytable
where updatedOn >= '11/12/2012' AND updatedOn <= '11/12/2012 23:59:59.999'
As when you write '11/12/2012' it means '11/12/2012 00:00:00', so you also need to add the time to the string
EDIT: as #Dems pointed out I also needed to take milli seconds into account
You should use DATE function -
SELECT
*
FROM
mytable
WHERE
DATE(updatedOn) = '2012-12-11'
How can I fetch MySQL data after a specific timestamp? What should the query look like?
mysql_query("SELECT * FROM table where TheNameOfTimestampColumn > than the date");
SELECT * FROM table WHERE TheNameOfTimestampColumn > '2009-01-28 21:00:00'
SELECT * FROM table WHERE TheNameOfTimestampColumn > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)
You can select this by :
select * from table_name where date_column > "2001-01-01 00:00:00"
or if you need data within certain time frame then you can try using between key word such as:
select * from table_name where date_column
between "2018-01-04 00:00:00" and "2018-01-04 11:59:59";
Note that date format should be in YYYY-MM-DD HH:MM:SS
If you're using a unix timestamp, you can do the following:
SELECT * FROM table WHERE TheNameOfTimestampColumn > FROM_UNIXTIME(your_time_stamp_here)
You can use MySQL DATE function like below
For instance, if you want results after 2017-09-05
SELECT DATE(timestamp_field) as date FROM stocks_annc WHERE DATE(timestamp_field) >= '2017-09-05'
Make sure to wrap the date within single quotation ''
Hope this helps.
How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'