Skip to content
Related Articles

Related Articles

Comparing Timestamp Dates With Date-Only Parameter in SQL

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 25 Jan, 2022

In this article, we are going to learn a SQL query to compare timestamp dates with date-only parameters. To do this, we will first create a table in a database and add timestamp values to it. After that, we use the SELECT command in SQL with a condition(using WHERE command) to get some values from the table. For comparison, will convert DateTime values to date values using the CAST() function.


This function in SQL converts data from one data type to another.

Step 1: Making a new database

To create a new database, we will use the following query

Query :


Step 2: Specifying the usage of the database

We use this query to use the sql_geek database.

Query :

USE sql_geek;

Step 3: Making a new table in the database 

We use the CREATE TABLE command in SQL to create a new table. This is the query used in this example:

Query :

CREATE TABLE geek_table(
datetime_col DATETIME);

Step 4: Add data to the table

The INSERT INTO statement in SQL is used to insert data into a table. The query we will use is:


INSERT INTO geek_table(datetime_col)
('2001-11-15 09:50:00 PM'),
('2006-08-09 04:30:00 PM'),
('2001-11-15 12:30:00 AM'),
('2005-06-03 06:22:11 PM'),
('2004-01-01 07:42:17 AM');

Step 5: Comparison and selection of particular data from the table

In this step, we will be selecting data from the table for which the SELECT command will be used. We will be selecting data according to the condition that the Date only parameter of these Datetime values is EQUAL to a particular date value, for which the WHERE clause will be used. For the conversion of Datetime values to Date values, the CAST() function will be used. Its syntax is :


CAST(value AS datatype)


SELECT * FROM geek_table
CAST(datetime_col AS DATE) = '2001-11-15';


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!