7/15/25, 9:38 AM Date Functions in SQL Server and MySQL
Tutorials Exercises Services Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
SQL Working With Dates
❮ Previous Next ❯
SQL Dates
The most difficult part when working with dates is to be sure that the format of the date
you are trying to insert, matches the format of the date column in the database.
As long as your data contains only the date portion, your queries will work as expected.
However, if a time portion is involved, it gets more complicated.
SQL Date Data Types
MySQL comes with the following data types for storing a date or a date/time value in
the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
YEAR - format YYYY or YY
SQL Server comes with the following data types for storing a date or a date/time value
in the database:
https://www.w3schools.com/sql/sql_dates.asp 1/6
7/15/25, 9:38 AM Date Functions in SQL Server and MySQL
DATE - format YYYY-MM-DD
Tutorials Exercises Services
DATETIME - format: YYYY-MM-DD HH:MI:SS
Sign In
SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
TIMESTAMP - format: a unique number
Note: The date types are chosen for a column when you create a new table in your
database!
SQL Working with Dates
Look at the following table:
Orders Table
OrderId ProductName OrderDate
1 Geitost 2008-11-11
2 Camembert Pierrot 2008-11-09
3 Mozzarella di Giovanni 2008-11-11
4 Mascarpone Fabioli 2008-10-29
Now we want to select the records with an OrderDate of "2008-11-11" from the table
above.
We use the following SELECT statement:
SELECT * FROM Orders WHERE OrderDate='2008-11-11'
The result-set will look like this:
OrderId ProductName OrderDate
1 Geitost 2008-11-11
3 Mozzarella di Giovanni 2008-11-11
https://www.w3schools.com/sql/sql_dates.asp 2/6
7/15/25, 9:38 AM Date Functions in SQL Server and MySQL
Tutorials Exercises Services
Note: Two dates can easily be compared if there is no time component involved!
Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Now, assume that the "Orders" table looks like this (notice the added time-component in
the "OrderDate" column):
OrderId ProductName OrderDate
1 Geitost 2008-11-11 13:23:44
2 Camembert Pierrot 2008-11-09 15:45:21
3 Mozzarella di Giovanni 2008-11-11 11:12:01
4 Mascarpone Fabioli 2008-10-29 14:56:59
If we use the same SELECT statement as above:
SELECT * FROM Orders WHERE OrderDate='2008-11-11'
we will get no result! This is because the query is looking only for dates with no time
portion.
Tip: To keep your queries simple and easy to maintain, do not use time-components in
your dates, unless you have to!
?
Exercise
Which of the following is the correct format for the DATE data type?
YYYY/MM/DD
MM-DD-YYYY
https://www.w3schools.com/sql/sql_dates.asp 3/6
7/15/25, 9:38 AM Date Functions in SQL Server and MySQL
YYYY-MM-DD
Tutorials Exercises Services Sign In
HTML
DD-MM-YYYY
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Submit Answer »
❮ Previous Next ❯
Track your progress - it's free! Sign Up Log in
COLOR PICKER
https://www.w3schools.com/sql/sql_dates.asp 4/6
7/15/25, 9:38 AM Date Functions in SQL Server and MySQL
Tutorials Exercises Services Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
PLUS SPACES
GET CERTIFIED FOR TEACHERS
FOR BUSINESS CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
https://www.w3schools.com/sql/sql_dates.asp 5/6
7/15/25, 9:38 AM Date Functions in SQL Server and MySQL
HTML Colors
Tutorials Java Reference
Exercises
Angular Reference
Services Sign In
jQuery Reference
HTML
CSS JAVASCRIPT SQL
Top Examples PYTHON JAVA PHP Get HOW TO
Certified W3.CSS C
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.
https://www.w3schools.com/sql/sql_dates.asp 6/6