Вы находитесь на странице: 1из 3

Date Functions in SQL Server and MySQL

http://www.w3schools.com/sql/sql_dates.asp

ADVERTISEMENTS XML Certification Download XML editor Custom Programming WEB HOSTING SQL Basic SQL HOME SQL Intro SQL Syntax SQL Select SQL Distinct SQL Where SQL And & Or SQL Order By SQL Insert SQL Update SQL Delete SQL Demo SQL Try It SQL Advanced SQL Top SQL Like SQL Wildcards SQL In SQL Between SQL Alias SQL Joins SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL Union SQL Select Into SQL Create DB SQL Create Table SQL Constraints SQL Not Null SQL Unique SQL Primary Key SQL Foreign Key SQL Check SQL Default SQL Create Index SQL Drop SQL Alter SQL Increment SQL Views SQL Dates SQL Nulls SQL isnull() SQL Data Types SQL Functions SQL Functions SQL avg() SQL count() SQL first() SQL last() SQL max() SQL min() SQL sum() SQL Group By SQL Having SQL ucase() SQL lcase() SQL mid() SQL len() SQL round() SQL now() SQL format() SQL Quick Ref SQL Hosting SQL Summary SQL Quiz SQL Quiz

SQL Date Functions

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 complicated. Before talking about the complications of querying for dates, we will look at the most important built-in functions for working with dates.

MySQL Date Functions


The following table lists the most important built-in date functions in MySQL: Function NOW() CURDATE() CURTIME() DATE() EXTRACT() DATE_ADD() DATE_SUB() DATEDIFF() DATE_FORMAT() Description Returns the current date and time Returns the current date Returns the current time Extracts the date part of a date or date/time expression Returns a single part of a date/time Adds a specified time interval to a date Subtracts a specified time interval from a date Returns the number of days between two dates Displays date/time data in different formats WEB BUILDING

SQL Server Date Functions


The following table lists the most important built-in date functions in SQL Server: Function GETDATE() DATEPART() DATEADD() DATEDIFF() CONVERT() Description Returns the current date and time Returns a single part of a date/time Adds or subtracts a specified time interval from a date Returns the time between two dates Displays date/time data in different formats EDUCATION

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:MM:SS TIMESTAMP - format: YYYY-MM-DD HH:MM: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: DATE - format YYYY-MM-DD DATETIME - format: YYYY-MM-DD HH:MM:SS SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS TIMESTAMP - format: a unique number Note: The date types are chosen for a column when you create a new table in your database! For an overview of all data types available, go to our complete Data Types reference.

SQL Working with Dates


You can compare two dates easily if there is no time component involved!

1 of 3

1/29/2009 11:53 AM

Date Functions in SQL Server and MySQL

http://www.w3schools.com/sql/sql_dates.asp

Selected Reading Web Statistics Web Glossary Web Hosting Web Quality W3Schools Tutorials W3Schools Forum Helping W3Schools

Assume we have the following "Orders" table: OrderId 1 2 3 4 ProductName Geitost Camembert Pierrot Mozzarella di Giovanni Mascarpone Fabioli OrderDate 2008-11-11 2008-11-09 2008-11-11 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 1 3 ProductName Geitost Mozzarella di Giovanni OrderDate 2008-11-11 2008-11-11

Now, assume that the "Orders" table looks like this (notice the time component in the "OrderDate" column): OrderId 1 2 3 4 ProductName Geitost Camembert Pierrot Mozzarella di Giovanni Mascarpone Fabioli OrderDate 2008-11-11 13:23:44 2008-11-09 15:45:21 2008-11-11 11:12:01 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: If you want to keep your queries simple and easy to maintain, do not allow time components in your dates!

Product Spotlight
Why are you still hand-coding? Generate data entry and reporting .NET Web apps in minutes, straight from your database. Visually stunning, easy to customize and ready to deploy. Download Now!

6 ways to take your site to the next level with Ektron:

Ektron CMS400.NET Version 7.6 delivers all of the flexibility and features you need to deploy the Web site you want, quickly and efficiently. learn more...

Create site stickiness through social networking. Keep it personal, relevant and interactive and they'll come back for more. Keep it open. Your site needs to be ready and able to connect to outside services. Ektron's open API gives you maximum flexibility. Streamline content and document management. Users need to quickly and intuitively find and add information. Empower your content authors. Reduce IT bottlenecks by allowing business users to create and edit Web content and forms. Climb to the top of search rankings. SEO tools, URL aliasing and eCommerce for your digital marketing strategy

2 of 3

1/29/2009 11:53 AM

Date Functions in SQL Server and MySQL

http://www.w3schools.com/sql/sql_dates.asp

Add powerful W eb 2.0 tools like blogs, wikis, forums, geo-mapping, rating systems and RSS feeds easily.

See why there are 20,000+ Ektron integrations worldwide. Request an INSTANT DEMO or download a FREE TRIAL today.

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2009 by Refsnes Data. All Rights Reserved. W3Schools was converted to XHTML in 1999

3 of 3

1/29/2009 11:53 AM