I am trying to filter between two dates on a SQL server from a PHP process.
This is the query:
select * from webStocks where FECHAMODIFICADO between '2020-06-03 17:16:02' and '2020-06-04 17:16:03' ORDER BY webStocks.FECHAMODIFICADO DESC
This is the result:
The result is not what I want. In the table I have the following information and it should be the result.
What am I doing wrong in the query?:(
Advertisement
Answer
I’d try to make sure the date column actually contains ‘timestamp’ data type.
If it doesn’t, the following code should fix it:
SELECT *
FROM webStocks
where CAST(FECHAMODIFICADO AS timestamp) BETWEEN '2020-06-03 17:16:02' AND '2020-06-04 17:16:03'
ORDER BY webStocks.FECHAMODIFICADO DESC
You can see more information about this kind of statements here. (this solution is valid mostly for MySQL, but will probably work with either CAST or CONVERT statement with other SQL servers).