How to Use Like Query with Between Query in MySQL - php

I have a table with field name counselorDate which have value with date and time.But i want to get data between to dates only(not with time).I used following query.But its not worked. please help me
SELECT
*
FROM poolMainEnqDetails
WHERE counselorDate
LIKE BETWEEN ('2014-01-01%' AND '2014-01-03%')

The syntax of the BETWEEN is flawed, it should be
BETWEEN '2014-01-01%' AND '2014-01-03%'
Also, what does your sql management tool (PHPMyAdmin, SQL Workbench, etc...) tells you ? If it doesn't work, it should either give an error, either give a result you're not expecting.

If you compare a date against a datetime the that will be converted to datetime.
With that in mind you'll get the folowing where clause:
SELECT
*
FROM poolMainEnqDetails
WHERE counselorDate >= '2014-01-01'
AND counselorDate < '2014-01-04'

Related

Filter MySQL Query By DateTime Stored As Varchar

I'm trying to filter a MySQL query by a field that stores datetime values, but uses a varchar data type.
Example data:
16:56:41 01/14/21 GMT
Example code:
SELECT * FROM table_name WHERE STR_TO_DATE(datetime,'%H:%i:%S %m/%d/%y') BETWEEN '00:00:00 01/14/21' AND '23:59:59 01/14/21'"
The query is currently returning nothing at all. However the code below works:
SELECT * FROM table_name
Any thoughts would be much appreciated.
Your use of STR_TO_DATE is correct, but your date literals are off. Fix them, and your query should work:
SELECT *
FROM table_name
WHERE STR_TO_DATE(datetime,'%H:%i:%S %m/%d/%y') >= '2021-01-14' AND
STR_TO_DATE(datetime,'%H:%i:%S %m/%d/%y') < '2021-01-15';
The above assumes that you want records occurring on 14th January, 2021, proper. Note that we don't need to even include the H:M:S components and can just use date literals.
It would be best to make your datetime column a proper datetime type column, rather than text. This would avoid the need to use STR_TO_DATE.

Get value between to hours from mysql using php

I'm trying to get a specific show description from my DB but I really don't know how, I know i'm new into this, the table (guide) have 2 DATETIME values "start" and "end"
If I use this:
$sql = mysql_query("SELECT * FROM guide WHERE start >= CURDATE()");
Only return the first value from the table, and inclusive its the wrong value, please I need some help to get this, because I don't find a solution from other on this web
You should be able to use mysql's between function to pull the records in the current time range.
select * from guide where now() between start and end
To limit the returns you can add in additional parameters, this may give you back no results though so have a default value.
select * from guide where channel = $channel_ID and now() between start and end
You also should look into parameterized queries and updating your driver. Having variables in your query isn't the best practice.

selecting rows from database between two dates giving wrong results

Selecting rows from database between two dates giving wrong results, the below query not working for me. I tried some answers, but this one not giving the correct results.I think, i am missing somewhere.
SELECT * FROM Table WHERE Date BETWEEN '07/10/2015' AND '07/14/2015'
changed to
SELECT * FROM Table WHERE Date BETWEEN '07-10-2015' AND '07-14-2015'
still not working!
That's right, you can not use BETWEEN statement when the data type format is not a DATE or DATETIME, you must change the data type first.
BTW I just realized that even data type is a DATE/DATETIME format you can't use / in the SQL statement itself when using MySQL, versus SQL you can use / when the column data type is DATE/DATETIME. Just correct me if I'm wrong...
Use the default date format YYYY-MM-DD
SELECT * FROM your_Table
WHERE Date BETWEEN '2015-07-10' AND '2015-07-14'
Erm, you're searching on "Date" but are you meant to be searching on "CreateDate"? That's what your image shows.

In MySQL, how to convert this string to a date?

I am creating a mysql db with a php frontend. The data it will use is extracted from another larger db and contains a date/time field which looks like this - 20120301073136 - which records when an event happened.
I understand that this might be a UNIX timestamp? Not sure.
I want to be show this field in the tables in my PHP webpage as a readable date and time -
ie something like 01-Mar-2012 07:31:36 or similar
Should I try and convert it with SQL command or let PHP format it? And, what is the code to do so?
BTW, it is important that I can sort the data (in SQL and in the PHP table) into date order - ie in the order that these events happened.
Thanks in advance for your help - Ive learnt a lot here already
J
You can convert it to a datetime directly in your SQL query. Example:
select cast(20120301073136 as datetime)
You can also order that with no need to convert it since it is a number in the format YYYYMMDDHHmmss
select * from yourTable
order by yourDateTimeField
You should make use of the MYSQL DATE functions. Check the docs before asking simple questions. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html.
Also you can sort the dates directly in your query using ORDER BY.

Query records based on datetime column

I have a column of type datetime in MySql table. Stored values are like this:
2008-02-15 17:21:56
2008-02-15 17:22:02
2008-02-15 17:22:03
Now I want to query some records based on this column but I have only date part not time part.
So I am querying like this in zend.
$select->where('tableName.columnName = ?', '2008-02-15' );
But it does not match any record. How can I query without time part.
Thanks
try this WHERE DATE(tableName.columnName) = '2008-02-15'
The answer #Shota has provided will work, however it will prevent MySQL from using any indexes on the datetime column due to the DATE function having to be called on every row of the table to see if it matches.
If you hard code the time range into the query it will give the same result and still allow indexes to be used.
eg. $select->where('tableName.columnName >= ? AND tableName.columnName <= ?', '2008-02-15 00:00:00', '2008-02-15 23:59:59' );
Please note i may have the php syntax wrong but you should get the idea.

Categories