Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
 
 


 

Mysql date API

                         

API stand for Application Programming Interface. API in SQL are used in accessing and processing the data from the database. The different API are used in  Mysql are as follows :

TO_DAYS() - Calculates the day number corresponding to a specific date between year 0 and the specified date.

FROM_DAYS() -Calculates the date corresponding to a day number. This function accepts a day number and returns the corresponding date value.

DATE_ADD()
- Adds a specified interval to a date and returns a new date

DATE_SUB() - Subtracts a specified interval from a date and returns a new date

PERIOD_DIFF() -Calculates the difference (in months) between two dates

PERIOD_ADD() - Adds an interval (in months) to a date and returns a new date

 

Understand with Example

The Tutorial enlighten you on common API used in Mysql  date. In this example, we elaborate  you an example on  each API, that is sufficient to understand them. 

The given below Query  helps you to find out the numbers of days from 0  to the day till now. 

To_Days  ( ) : This is used to calculate the day number corresponding date from year 0 and the specified date.

mysql> select To_days(now())as Days;

Output:-

+--------+
| Days   |
+--------+
| 733761 |
+--------+
1 row in set (0.00 sec)

Query FROM_DAYS():-

The given below Query calculate you the date corresponding to a day number. The given Query  have a function that accept a day number and return you the corresponding date value.

From_Days ( ) : The given below Query is used to calculate the date corresponding to a day number and return you the corresponding date value.

mysql> select From_days(733761)as Date;

Output:-

+------------+
| Date       |
+------------+
| 2008-12-20 |
+------------+
1 row in set (0.00 sec)

Query DATE_ADD() :-

The given below Query adds a specified interval to a current date. In this Query we add a interval of one year to the current date. 

DATE_ADD() : The Query adds a specified interval to a date and returns a new date

 

mysql> select date_add(curdate(),interval 1 year)as Dateafter1Year;

Output:-

+----------------+
| Dateafter1Year |
+----------------+
| 2009-12-20     |
+----------------+
1 row in set (0.00 sec)

Query DATE_SUB() :-

The below Query is used to subtract a interval of 1 year from the current date.

DATE_SUB() : The DATE_SUB ( ) Subtracts a specified interval from a date and returns a new date

mysql> select date_sub(curdate(),interval 1 year)as DatebeforeYear;

Output:-

+----------------+
| DatebeforeYear |
+----------------+
| 2007-12-20     |
+----------------+
1 row in set (0.00 sec)

Query PERIOD_DIFF():-

The below Syntax is used to show the difference between the two date. The given Query is used to show the period difference between the two consecutive year.

Period_Diff ( ) : The Period_Diff  is used to calculate the period difference between any two dates. 

mysql> select period_diff('2008-12-22','2007-12-22')as Perioddifference;

Output:-

+------------------+
| Perioddifference |
+------------------+
|                1 |
+------------------+
1 row in set, 2 warnings (0.00 sec)

Query PERIOD_ADD():-

The below Query add the interval in month to the specified date given and return you a new days.

PERIOD_ADD() : The Period _ADD adds an interval (in months) to a date and returns a new date

mysql> select period_add(200808,03)as PeriodAdd;

Output:-

+-----------+
| PeriodAdd |
+-----------+
|    200811 |
+-----------+
1 row in set (0.00 sec)

                         

» View all related tutorials
Related Tags: sql mysql c orm form time make format display value this create for ie example values exam e it mms

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.