
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Compare DATE string with string from MySQL DATETIME field?
You can compare DATE string with string from DATETIME field with the help of DATE() function in MySQL.The syntax is as follows −
select *from yourTableName where DATE(yourColumnName) = ’anyDateString’;
To understand the above syntax, let us create a table and set some datetime values in the table. The query to create a table −
mysql> create table DateTimeDemo −> ( −> ArrivalTime datetime −> ); Query OK, 0 rows affected (0.61 sec)
Let us insert some records in the table with the help of insert command. The following is the query to insert records −
mysql> insert into DateTimeDemo values(now()); Query OK, 1 row affected (0.11 sec) mysql> insert into DateTimeDemo values(date_add(now(),interval 2 year)); Query OK, 1 row affected (0.47 sec) mysql> insert into DateTimeDemo values(date_add(now(),interval 1 year)); Query OK, 1 row affected (0.19 sec) mysql> insert into DateTimeDemo values(date_add(now(),interval 4 year)); Query OK, 1 row affected (0.13 sec) mysql> insert into DateTimeDemo values(date_add(now(),interval -2 year)); Query OK, 1 row affected (0.17 sec) mysql> insert into DateTimeDemo values(date_add(now(),interval -1 year)); Query OK, 1 row affected (0.17 sec)
Display all records with the help of select statement. The query to display all records from the table −
mysql> select *from DateTimeDemo;
The following is the output −
+---------------------+ | ArrivalTime | +---------------------+ | 2018-12-06 10:12:45 | | 2020-12-06 10:13:10 | | 2019-12-06 10:13:21 | | 2022-12-06 10:13:27 | | 2016-12-06 10:13:42 | | 2017-12-06 10:13:50 | +---------------------+ 6 rows in set (0.00 sec)
Now implement the syntax we discussed above for comparing string with date time field. The query is as follows −
mysql> select *from DateTimeDemo where date(ArrivalTime) = '2022-12-06';
The following is the output −
+---------------------+ | ArrivalTime | +---------------------+ | 2022-12-06 10:13:27 | +---------------------+ 1 row in set (0.14 sec)
- Related Articles
- Converting a date in MySQL from string field?
- How to update date of datetime field with MySQL?
- How to compare DateTime Column with only Date not time in MySQL?
- Compare only day and month with date field in MySQL?
- How to combine date and time from different MySQL columns to compare with the entire DateTime?
- Extracting only date from datetime field in MySQL and assigning it to PHP variable?
- How to extract date from string in MySQL?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- Convert from varchar to datetime and compare in MySQL?
- Convert DateTime Value into String in MySQL?
- MySQL Query to convert from datetime to date?
- Create DATETIME from DATE and TIME in MySQL?
- Get only the date from datetime in MySQL?
- Insert datetime into another datetime field in MySQL?
- How to select only MySQL date from datetime column?

Advertisements