Home Sql SQL Between Datetime
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

SQL Between Datetime

Advertisement
The Tutorial illustrate an example from SQL Between Date time. The example create a table 'Stu_Table'.

SQL Between Datetime

     

The Tutorial illustrate an example from SQL Between Date time. The example create a table 'Stu_Table'. The create table statement is used to create a table 'Stu_Table'. The data type for the field Stu_Dob is date time. The Date Time structure tell you an instant in time, hold both date and a time. 

Create Table Stu_Table

Create Table Stu_Table (Stu_Id varchar(2), Stu_Name varchar(15), Stu_Dob datetime);

Insert Data Into Stu_Table

The insert into add the records or rows to the table 'Stu_Table'.

insert into stu_table values('1', 'Komal', '1984-10-27 02:10:00');
insert into stu_table values('2', 'Ajay', '1985-04-19 12:10:25');
insert into stu_table values('3', 'santosh', '1986-11-16 23:46:34');

Stu_Table

+-----------+----------+---------------------+
| Stu_Table | Stu_Name | Stu_Dob             |
+-----------+----------+---------------------+
| 1         | Komal    | 1984-10-27 02:10:00 |
| 2         | Ajay     | 1985-04-19 12:10:25 |
| 3         | Santosh  | 1986-11-16 23:46:34 |
+-----------+----------+---------------------+

Query

The given below query return you the records from table 'Stu_Table' .The Where Clause restrict the records on the basis of Stu_Dob as specified in condition.

Select * From Stu_Table
Where Stu_Dob Between '1984-01-01 00:00:00' And '1986-1-1 00:00:00';

Result

+-----------+----------+---------------------+
| Stu_Table | Stu_Name | Stu_Dob             |
+-----------+----------+---------------------+
| 1         | Komal    | 1984-10-27 02:10:00 |
| 2         | Ajay     | 1985-04-19 12:10:25 |
+-----------+----------+---------------------+
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 20, 2008

Related Tutorials

Ask Questions?    Discuss: SQL Between Datetime   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Will Mainwaring
April 14, 2011
SQL Between Datetime

If you want to include dates on the same day as the 'To' date, you need to change '1986-1-1 00:00:00' to '1986-1-1 23:59:59' Hope this helps someone!
harsh
July 1, 2011
MySql Query

1984-10-27 to 2011-04-19 wht is the age how i can calculate .....? in mysql
Neelen
October 24, 2012
datetime function

Hi, I have tried the above using your example but keep getting "data type mismatch in criteria expression" please help
DMCA.com