I have a table with a column called date
, as a varchar. The contents are specifically formatted like \'March 11, 2011\'.
How can I select the results have
If your two dates are in variables such as $date_begin
and $date_end
, you could use an SQL query like the following one, to get data that's between those dates :
select *
from your_table
where date between '$date_begin' and '$date_end'
(Not sure I quite understand the question)
If you want to get the rows that correspond to the current date, you'll need to variables :
$current
; that would be like 2011-03-20
-- which means 2011-03-20 00:00:00
$next
; that would be like 2011-03-21
-- which means 2011-03-21 00:00:00
And you'll use a query like this one :
select *
from your_table
where date >= '$current' and date < '$next'
To select data that has its date which is greater or equal that today ; and that's before tomorrow.
In any case, try to not apply an SQL function on your date column ; don't do anything like this :
where some_function(date) >= ...
Doing this, your database engine would have to apply that function to all lines of your table, not using any index you might have on the date
column -- and this will result in bad performances.
Yes, you can use the DATE function along with CURDATE.
SELECT * FROM tbl WHERE DATE(your_date_column) = CURDATE()
This will select all rows where your_date_column
is todays date.
If you want to select all rows where your_date_column
is between two dates, you can simply use BETWEEN:
SELET * FROM tbl WHERE your_date_column BETWEEN '20111-03-20' AND '2011-03-30'
I'm blindly assuming MySQL here, because it makes sense in the context of this question. Nobody using another database engine would dare create this problem in the first place.
STR_TO_DATE to the rescue! Given the format "March 01, 2000", the following conversion should work.
SELECT STR_TO_DATE(column_name, '%M %d, %Y')
FROM TableName
WHERE ...
ORDER BY STR_TO_DATE(column_name, '%M %d, %Y')
You may need to adjust the format string a bit.