<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dreamvib ~ music lounge &#187; mysql</title>
	<atom:link href="http://music.dreamvib.com/category/programming/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://music.dreamvib.com</link>
	<description></description>
	<lastBuildDate>Fri, 12 Jun 2009 08:31:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Date and Time Functions in mysql</title>
		<link>http://music.dreamvib.com/programming/mysql/date-and-time-functions-in-mysql/</link>
		<comments>http://music.dreamvib.com/programming/mysql/date-and-time-functions-in-mysql/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 11:34:44 +0000</pubDate>
		<dc:creator>dreamvib</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[date and time function]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://blog.dreamvib.com/?p=18</guid>
		<description><![CDATA[source:http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
Date and Time Functions
       This section describes the functions that can be used to       manipulate temporal values. See       SectionÂ 9.3, â€œDate and Time Typesâ€, for a description of the       range of values [...]]]></description>
			<content:encoded><![CDATA[<p>source:<a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html">http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html</a></p>
<h2 class="title">Date and Time Functions</h2>
<p><a class="indexterm" name="id2837175"></a><a class="indexterm" name="id2837184"></a>       This section describes the functions that can be used to       manipulate temporal values. See       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html" title="9.3.Â Date and Time Types">SectionÂ 9.3, â€œDate and Time Typesâ€</a>, for a description of the       range of values each date and time type has and the valid formats       in which values may be specified.</p>
<p class="informaltable">
<table border="1">
<thead>
<th><span class="bold"><strong>Name</strong></span></th>
<th><span class="bold"><strong>Description</strong></span></th>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate"><code class="literal">ADDDATE()</code></a>(v4.1.1)</td>
<td>Add dates</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_addtime"><code class="literal">ADDTIME()</code></a>(v4.1.1)</td>
<td>Add time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz"><code class="literal">CONVERT_TZ()</code></a>(v4.1.3)</td>
<td>Convert from one timezone to another</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate"><code class="literal">CURDATE()</code></a></td>
<td>Return the current date</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code>, <code class="literal">CURRENT_DATE</code></a></td>
<td>Synonyms for CURDATE()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-time"><code class="literal">CURRENT_TIME()</code>, <code class="literal">CURRENT_TIME</code></a></td>
<td>Synonyms for CURTIME()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code>, <code class="literal">CURRENT_TIMESTAMP</code></a></td>
<td>Synonyms for NOW()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curtime"><code class="literal">CURTIME()</code></a></td>
<td>Return the current time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a></td>
<td>Add two dates</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a></td>
<td>Format date as specified</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a></td>
<td>Subtract two dates</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date"><code class="literal">DATE()</code></a>(v4.1.1)</td>
<td>Extract the date part of a date or datetime expression</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_datediff"><code class="literal">DATEDIFF()</code></a>(v4.1.1)</td>
<td>Subtract two dates</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_day"><code class="literal">DAY()</code></a>(v4.1.1)</td>
<td>Synonym for DAYOFMONTH()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayname"><code class="literal">DAYNAME()</code></a>(v4.1.21)</td>
<td>Return the name of the weekday</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofmonth"><code class="literal">DAYOFMONTH()</code></a></td>
<td>Return the day of the month (1-31)</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofweek"><code class="literal">DAYOFWEEK()</code></a></td>
<td>Return the weekday index of the argument</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofyear"><code class="literal">DAYOFYEAR()</code></a></td>
<td>Return the day of the year (1-366)</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_extract"><code class="literal">EXTRACT</code></a></td>
<td>Extract part of a date</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-days"><code class="literal">FROM_DAYS()</code></a></td>
<td>Convert a day number to a date</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a></td>
<td>Format date as a UNIX timestamp</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT()</code></a>(v4.1.1)</td>
<td>Return a date format string</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_hour"><code class="literal">HOUR()</code></a></td>
<td>Extract the hour</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_last-day"><code class="literal">LAST_DAY</code></a>(v4.1.1)</td>
<td>Return the last day of the month for the argument</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtime"><code class="literal">LOCALTIME()</code>, <code class="literal">LOCALTIME</code></a></td>
<td>Synonym for NOW()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP</code>, <code class="literal">LOCALTIMESTAMP()</code></a>(v4.0.6)</td>
<td>Synonym for NOW()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_makedate"><code class="literal">MAKEDATE()</code></a>(v4.1.1)</td>
<td>Create a date from the year and day of year</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_maketime"><code class="literal">MAKETIME</code></a>(v4.1.1)</td>
<td>MAKETIME()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_microsecond"><code class="literal">MICROSECOND()</code></a>(v4.1.1)</td>
<td>Return the microseconds from argument</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_minute"><code class="literal">MINUTE()</code></a></td>
<td>Return the minute from the argument</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_month"><code class="literal">MONTH()</code></a></td>
<td>Return the month from the date passed</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_monthname"><code class="literal">MONTHNAME()</code></a>(v4.1.21)</td>
<td>Return the name of the month</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a></td>
<td>Return the current date and time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_period-add"><code class="literal">PERIOD_ADD()</code></a></td>
<td>Add a period to a year-month</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_period-diff"><code class="literal">PERIOD_DIFF()</code></a></td>
<td>Return the number of months between periods</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_quarter"><code class="literal">QUARTER()</code></a></td>
<td>Return the quarter from a date argument</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sec-to-time"><code class="literal">SEC_TO_TIME()</code></a></td>
<td>Converts seconds to &#8216;HH:MM:SS&#8217; format</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_second"><code class="literal">SECOND()</code></a></td>
<td>Return the second (0-59)</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date"><code class="literal">STR_TO_DATE()</code></a>(v4.1.1)</td>
<td>Convert a string to a date</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subdate"><code class="literal">SUBDATE()</code></a></td>
<td>When invoked with three arguments a synonym for DATE_SUB()</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subtime"><code class="literal">SUBTIME()</code></a>(v4.1.1)</td>
<td>Subtract times</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a></td>
<td>Return the time at which the function executes</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time-format"><code class="literal">TIME_FORMAT()</code></a></td>
<td>Format as time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time-to-sec"><code class="literal">TIME_TO_SEC()</code></a></td>
<td>Return the argument converted to seconds</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time"><code class="literal">TIME()</code></a>(v4.1.1)</td>
<td>Extract the time portion of the expression passed</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timediff"><code class="literal">TIMEDIFF()</code></a>(v4.1.1)</td>
<td>Subtract time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestamp"><code class="literal">TIMESTAMP()</code></a>(v4.1.1)</td>
<td>With a single argument, this function returns the date or  datetime expression. With two arguments, the sum of the arguments</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampadd"><code class="literal">TIMESTAMPADD()</code></a>(v5.0.0)</td>
<td>Add an interval to a datetime expression</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff"><code class="literal">TIMESTAMPDIFF()</code></a>(v5.0.0)</td>
<td>Subtract an interval from a datetime expression</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_to-days"><code class="literal">TO_DAYS()</code></a></td>
<td>Return the date argument converted to days</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a></td>
<td>Return a UNIX timestamp</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-date"><code class="literal">UTC_DATE()</code></a>(v4.1.1)</td>
<td>Return the current UTC date</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-time"><code class="literal">UTC_TIME()</code></a>(v4.1.1)</td>
<td>Return the current UTC time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-timestamp"><code class="literal">UTC_TIMESTAMP()</code></a>(v4.1.1)</td>
<td>Return the current UTC date and time</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a></td>
<td>Return the week number</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_weekday"><code class="literal">WEEKDAY()</code></a></td>
<td>Return the weekday index</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_weekofyear"><code class="literal">WEEKOFYEAR()</code></a>(v4.1.1)</td>
<td>Return the calendar week of the date (1-53)</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_year"><code class="literal">YEAR()</code></a></td>
<td>Return the year</td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_yearweek"><code class="literal">YEARWEEK()</code></a></td>
<td>Return the year and week</td>
</tr>
</table>
<p>Here is an example that uses date functions. The following query       selects all rows with a <em class="replaceable"><code>date_col</code></em> value       from within the last 30 days:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT <em class="replaceable"><code>something</code></em> FROM <em class="replaceable"><code>tbl_name</code></em></code></strong>
    -&gt; <strong class="userinput"><code>WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) &lt;= <em class="replaceable"><code>date_col</code></em>;</code></strong></pre>
<p>Note that the query also selects rows with dates that lie in the       future.</p>
<p>Functions that expect date values usually accept datetime values       and ignore the time part. Functions that expect time values       usually accept datetime values and ignore the date part.</p>
<p>Functions that return the current date or time each are evaluated       only once per query at the start of query execution. This means       that multiple references to a function such as       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a> within a single query always       produce the same result (for our purposes a single query also       includes a call to a stored routine or trigger and all       sub-routines called by that routine/trigger). This principle also       applies to <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate"><code class="literal">CURDATE()</code></a>,       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curtime"><code class="literal">CURTIME()</code></a>,       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-date"><code class="literal">UTC_DATE()</code></a>,       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-time"><code class="literal">UTC_TIME()</code></a>,       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-timestamp"><code class="literal">UTC_TIMESTAMP()</code></a>, and to any of       their synonyms.</p>
<p>The <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code></a>,       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-time"><code class="literal">CURRENT_TIME()</code></a>,       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code></a>, and       <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a> functions return       values in the connection&#8217;s current time zone, which is available       as the value of the <code class="literal">time_zone</code> system variable.       In addition, <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a>       assumes that its argument is a datetime value in the current time       zone. See <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" title="8.9.Â MySQL Server Time Zone Support">SectionÂ 8.9, â€œMySQL Server Time Zone Supportâ€</a>.</p>
<p>Some date functions can be used with â€œ<span class="quote">zero</span>â€ dates or       incomplete dates such as <code class="literal">'2001-11-00'</code>, whereas       others cannot. Functions that extract parts of dates typically       work with incomplete dates. For example:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DAYOFMONTH('2001-11-00'), MONTH('2005-00-00');</code></strong>
        -&gt; 0, 0</pre>
<p>Other functions expect complete dates and return       <code class="literal">NULL</code> for incomplete dates. These include       functions that perform date arithmetic or that map parts of dates       to names. For example:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('2006-05-00',INTERVAL 1 DAY);</code></strong>
        -&gt; NULL
mysql&gt; <strong class="userinput"><code>SELECT DAYNAME('2006-05-00');</code></strong>
        -&gt; NULL</pre>
<p class="itemizedlist">
<ul>
<li><a name="function_adddate"></a>           <a class="indexterm" name="id2838391"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate"><code class="literal">ADDDATE(<em class="replaceable"><code>date</code></em>,INTERVAL           <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em>)</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate"><code class="literal">ADDDATE(<em class="replaceable"><code>expr</code></em>,<em class="replaceable"><code>days</code></em>)</code></a>
<p>When invoked with the <code class="literal">INTERVAL</code> form of the           second argument, <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate"><code class="literal">ADDDATE()</code></a> is           a synonym for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a>. The           related function <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subdate"><code class="literal">SUBDATE()</code></a> is           a synonym for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a>. For           information on the <code class="literal">INTERVAL</code>           <em class="replaceable"><code>unit</code></em> argument, see the discussion           for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);</code></strong>
        -&gt; '1998-02-02'
mysql&gt; <strong class="userinput"><code>SELECT ADDDATE('1998-01-02', INTERVAL 31 DAY);</code></strong>
        -&gt; '1998-02-02'</pre>
<p>When invoked with the <em class="replaceable"><code>days</code></em> form of           the second argument, MySQL treats it as an integer number of           days to be added to <em class="replaceable"><code>expr</code></em>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT ADDDATE('1998-01-02', 31);</code></strong>
        -&gt; '1998-02-02'</pre>
</li>
<li><a name="function_addtime"></a>           <a class="indexterm" name="id2838577"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_addtime"><code class="literal">ADDTIME(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_addtime"><code class="literal">ADDTIME()</code></a> adds           <em class="replaceable"><code>expr2</code></em> to           <em class="replaceable"><code>expr1</code></em> and returns the result.           <em class="replaceable"><code>expr1</code></em> is a time or datetime           expression, and <em class="replaceable"><code>expr2</code></em> is a time           expression.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT ADDTIME('1997-12-31 23:59:59.999999',</code></strong>
    -&gt;                <strong class="userinput"><code>'1 1:1:1.000002');</code></strong>
        -&gt; '1998-01-02 01:01:01.000001'
mysql&gt; <strong class="userinput"><code>SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');</code></strong>
        -&gt; '03:00:01.999997'</pre>
</li>
<li><a name="function_convert-tz"></a>           <a class="indexterm" name="id2838701"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz"><code class="literal">CONVERT_TZ(<em class="replaceable"><code>dt</code></em>,<em class="replaceable"><code>from_tz</code></em>,<em class="replaceable"><code>to_tz</code></em>)</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz"><code class="literal">CONVERT_TZ()</code></a> converts a           datetime value <em class="replaceable"><code>dt</code></em> from the time           zone given by <em class="replaceable"><code>from_tz</code></em> to the time           zone given by <em class="replaceable"><code>to_tz</code></em> and returns the           resulting value. Time zones are specified as described in           <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" title="8.9.Â MySQL Server Time Zone Support">SectionÂ 8.9, â€œMySQL Server Time Zone Supportâ€</a>. This function returns           <code class="literal">NULL</code> if the arguments are invalid.</p>
<p>If the value falls out of the supported range of the           <code class="literal">TIMESTAMP</code> type when converted from           <em class="replaceable"><code>from_tz</code></em> to UTC, no conversion           occurs. The <code class="literal">TIMESTAMP</code> range is described in           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html" title="9.1.2.Â Overview of Date and Time Types">SectionÂ 9.1.2, â€œOverview of Date and Time Typesâ€</a>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');</code></strong>
        -&gt; '2004-01-01 13:00:00'
mysql&gt; <strong class="userinput"><code>SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');</code></strong>
        -&gt; '2004-01-01 22:00:00'</pre>
<p class="note" style="margin-left: 0.5in; margin-right: 0.5in">
<h3 class="title">Note</h3>
<p>To use named time zones such as <code class="literal">'MET'</code> or             <code class="literal">'Europe/Moscow'</code>, the time zone tables             must be properly set up. See             <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" title="8.9.Â MySQL Server Time Zone Support">SectionÂ 8.9, â€œMySQL Server Time Zone Supportâ€</a>, for instructions.</p>
<p>If you intend to use           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz"><code class="literal">CONVERT_TZ()</code></a> while other           tables are locked with <code class="literal">LOCK TABLES</code>, you           must also lock the <code class="literal">mysql.time_zone_name</code>           table.</li>
<li><a name="function_curdate"></a>           <a class="indexterm" name="id2838903"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate"><code class="literal">CURDATE()</code></a>
<p>Returns the current date as a value in           <code class="literal">'YYYY-MM-DD'</code> or <code class="literal">YYYYMMDD</code>           format, depending on whether the function is used in a string           or numeric context.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT CURDATE();</code></strong>
        -&gt; '1997-12-15'
mysql&gt; <strong class="userinput"><code>SELECT CURDATE() + 0;</code></strong>
        -&gt; 19971215</pre>
</li>
<li><a name="function_current-date"></a>           <a class="indexterm" name="id2838995"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-date"><code class="literal">CURRENT_DATE</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-date"><code class="literal">CURRENT_DATE</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code></a> are synonyms           for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate"><code class="literal">CURDATE()</code></a>.</li>
<li><a name="function_curtime"></a>           <a class="indexterm" name="id2839079"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curtime"><code class="literal">CURTIME()</code></a>
<p>Returns the current time as a value in           <code class="literal">'HH:MM:SS'</code> or           <code class="literal">HHMMSS.uuuuuu</code> format, depending on whether           the function is used in a string or numeric context. The value           is expressed in the current time zone.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT CURTIME();</code></strong>
        -&gt; '23:50:26'
mysql&gt; <strong class="userinput"><code>SELECT CURTIME() + 0;</code></strong>
        -&gt; 235026.000000</pre>
</li>
<li><a name="function_current-time"></a>           <a class="indexterm" name="id2839174"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-time"><code class="literal">CURRENT_TIME</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-time"><code class="literal">CURRENT_TIME()</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-time"><code class="literal">CURRENT_TIME</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-time"><code class="literal">CURRENT_TIME()</code></a> are synonyms           for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curtime"><code class="literal">CURTIME()</code></a>.</li>
<li><a name="function_current-timestamp"></a>           <a class="indexterm" name="id2839257"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code></a> are           synonyms for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a>.</li>
<li><a name="function_date"></a>           <a class="indexterm" name="id2839347"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date"><code class="literal">DATE(<em class="replaceable"><code>expr</code></em>)</code></a>
<p>Extracts the date part of the date or datetime expression           <em class="replaceable"><code>expr</code></em>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE('2003-12-31 01:02:03');</code></strong>
        -&gt; '2003-12-31'</pre>
</li>
<li><a name="function_datediff"></a>           <a class="indexterm" name="id2839427"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_datediff"><code class="literal">DATEDIFF(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_datediff"><code class="literal">DATEDIFF()</code></a> returns           <em class="replaceable"><code>expr1</code></em> â€“           <em class="replaceable"><code>expr2</code></em> expressed as a value in days           from one date to the other. <em class="replaceable"><code>expr1</code></em>           and <em class="replaceable"><code>expr2</code></em> are date or date-and-time           expressions. Only the date parts of the values are used in the           calculation.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');</code></strong>
        -&gt; 1
mysql&gt; <strong class="userinput"><code>SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');</code></strong>
        -&gt; -31</pre>
</li>
<li><a name="function_date-add"></a>           <a class="indexterm" name="id2839553"></a>            <a class="indexterm" name="id2839562"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD(<em class="replaceable"><code>date</code></em>,INTERVAL           <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em>)</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-sub"><code class="literal">DATE_SUB(<em class="replaceable"><code>date</code></em>,INTERVAL           <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em>)</code></a>
<p>These functions perform date arithmetic.           <em class="replaceable"><code>date</code></em> is a           <code class="literal">DATETIME</code> or <code class="literal">DATE</code> value           specifying the starting date. <em class="replaceable"><code>expr</code></em>           is an expression specifying the interval value to be added or           subtracted from the starting date.           <em class="replaceable"><code>expr</code></em> is a string; it may start with           a â€œ<span class="quote"><code class="literal">-</code></span>â€ for negative intervals.           <em class="replaceable"><code>unit</code></em> is a keyword indicating the           units in which the expression should be interpreted.</p>
<p>The <code class="literal">INTERVAL</code> keyword and the           <em class="replaceable"><code>unit</code></em> specifier are not case           sensitive.</p>
<p>The following table shows the expected form of the           <em class="replaceable"><code>expr</code></em> argument for each           <em class="replaceable"><code>unit</code></em> value.</p>
<p class="informaltable">
<table border="1">
<tr>
<td><em class="replaceable"><code>unit</code></em> <span class="bold"><strong>Value</strong></span></td>
<td><span class="bold"><strong>Expected</strong></span>                   <em class="replaceable"><code>expr</code></em>                   <span class="bold"><strong>Format</strong></span></td>
</tr>
<tr>
<td><code class="literal">MICROSECOND</code></td>
<td><code class="literal">MICROSECONDS</code></td>
</tr>
<tr>
<td><code class="literal">SECOND</code></td>
<td><code class="literal">SECONDS</code></td>
</tr>
<tr>
<td><code class="literal">MINUTE</code></td>
<td><code class="literal">MINUTES</code></td>
</tr>
<tr>
<td><code class="literal">HOUR</code></td>
<td><code class="literal">HOURS</code></td>
</tr>
<tr>
<td><code class="literal">DAY</code></td>
<td><code class="literal">DAYS</code></td>
</tr>
<tr>
<td><code class="literal">WEEK</code></td>
<td><code class="literal">WEEKS</code></td>
</tr>
<tr>
<td><code class="literal">MONTH</code></td>
<td><code class="literal">MONTHS</code></td>
</tr>
<tr>
<td><code class="literal">QUARTER</code></td>
<td><code class="literal">QUARTERS</code></td>
</tr>
<tr>
<td><code class="literal">YEAR</code></td>
<td><code class="literal">YEARS</code></td>
</tr>
<tr>
<td><code class="literal">SECOND_MICROSECOND</code></td>
<td><code class="literal">'SECONDS.MICROSECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">MINUTE_MICROSECOND</code></td>
<td><code class="literal">'MINUTES.MICROSECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">MINUTE_SECOND</code></td>
<td><code class="literal">'MINUTES:SECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">HOUR_MICROSECOND</code></td>
<td><code class="literal">'HOURS.MICROSECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">HOUR_SECOND</code></td>
<td><code class="literal">'HOURS:MINUTES:SECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">HOUR_MINUTE</code></td>
<td><code class="literal">'HOURS:MINUTES'</code></td>
</tr>
<tr>
<td><code class="literal">DAY_MICROSECOND</code></td>
<td><code class="literal">'DAYS.MICROSECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">DAY_SECOND</code></td>
<td><code class="literal">'DAYS HOURS:MINUTES:SECONDS'</code></td>
</tr>
<tr>
<td><code class="literal">DAY_MINUTE</code></td>
<td><code class="literal">'DAYS HOURS:MINUTES'</code></td>
</tr>
<tr>
<td><code class="literal">DAY_HOUR</code></td>
<td><code class="literal">'DAYS HOURS'</code></td>
</tr>
<tr>
<td><code class="literal">YEAR_MONTH</code></td>
<td><code class="literal">'YEARS-MONTHS'</code></td>
</tr>
</table>
<p>The values <code class="literal">QUARTER</code> and           <code class="literal">WEEK</code> are available beginning with MySQL           5.0.0.</p>
<p>MySQL allows any punctuation delimiter in the           <em class="replaceable"><code>expr</code></em> format. Those shown in the           table are the suggested delimiters. If the           <em class="replaceable"><code>date</code></em> argument is a           <code class="literal">DATE</code> value and your calculations involve           only <code class="literal">YEAR</code>, <code class="literal">MONTH</code>, and           <code class="literal">DAY</code> parts (that is, no time parts), the           result is a <code class="literal">DATE</code> value. Otherwise, the           result is a <code class="literal">DATETIME</code> value.</p>
<p>Date arithmetic also can be performed using           <code class="literal">INTERVAL</code> together with the           <a href="http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_plus"><code class="literal">+</code></a> or           <a href="http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_minus"><code class="literal">-</code></a>           operator:</p>
<pre class="programlisting"><code class="literal">date</code> + INTERVAL <em class="replaceable"><code>expr</code></em> <em class="replaceable"><code>unit</code></em>
<code class="literal">date</code> - INTERVAL <em class="replaceable"><code>expr</code></em> <em class="replaceable"><code>unit</code></em></pre>
<p><code class="literal">INTERVAL <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em></code> is allowed on either           side of the           <a href="http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_plus"><code class="literal">+</code></a> operator           if the expression on the other side is a date or datetime           value. For the           <a href="http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_minus"><code class="literal">-</code></a>           operator, <code class="literal">INTERVAL <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em></code> is allowed only on           the right side, because it makes no sense to subtract a date           or datetime value from an interval.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT '1997-12-31 23:59:59' + INTERVAL 1 SECOND;</code></strong>
        -&gt; '1998-01-01 00:00:00'
mysql&gt; <strong class="userinput"><code>SELECT INTERVAL 1 DAY + '1997-12-31';</code></strong>
        -&gt; '1998-01-01'
mysql&gt; <strong class="userinput"><code>SELECT '1998-01-01' - INTERVAL 1 SECOND;</code></strong>
        -&gt; '1997-12-31 23:59:59'
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1997-12-31 23:59:59',</code></strong>
    -&gt;                 <strong class="userinput"><code>INTERVAL 1 SECOND);</code></strong>
        -&gt; '1998-01-01 00:00:00'
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1997-12-31 23:59:59',</code></strong>
    -&gt;                 <strong class="userinput"><code>INTERVAL 1 DAY);</code></strong>
        -&gt; '1998-01-01 23:59:59'
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1997-12-31 23:59:59',</code></strong>
    -&gt;                 <strong class="userinput"><code>INTERVAL '1:1' MINUTE_SECOND);</code></strong>
        -&gt; '1998-01-01 00:01:00'
mysql&gt; <strong class="userinput"><code>SELECT DATE_SUB('1998-01-01 00:00:00',</code></strong>
    -&gt;                 <strong class="userinput"><code>INTERVAL '1 1:1:1' DAY_SECOND);</code></strong>
        -&gt; '1997-12-30 22:58:59'
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1998-01-01 00:00:00',</code></strong>
    -&gt;                 <strong class="userinput"><code>INTERVAL '-1 10' DAY_HOUR);</code></strong>
        -&gt; '1997-12-30 14:00:00'
mysql&gt; <strong class="userinput"><code>SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);</code></strong>
        -&gt; '1997-12-02'
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1992-12-31 23:59:59.000002',</code></strong>
    -&gt;            <strong class="userinput"><code>INTERVAL '1.999999' SECOND_MICROSECOND);</code></strong>
        -&gt; '1993-01-01 00:00:01.000001'</pre>
<p>If you specify an interval value that is too short (does not           include all the interval parts that would be expected from the           <em class="replaceable"><code>unit</code></em> keyword), MySQL assumes that           you have left out the leftmost parts of the interval value.           For example, if you specify a <em class="replaceable"><code>unit</code></em>           of <code class="literal">DAY_SECOND</code>, the value of           <em class="replaceable"><code>expr</code></em> is expected to have days,           hours, minutes, and seconds parts. If you specify a value like           <code class="literal">'1:10'</code>, MySQL assumes that the days and           hours parts are missing and the value represents minutes and           seconds. In other words, <code class="literal">'1:10' DAY_SECOND</code>           is interpreted in such a way that it is equivalent to           <code class="literal">'1:10' MINUTE_SECOND</code>. This is analogous to           the way that MySQL interprets <code class="literal">TIME</code> values           as representing elapsed time rather than as a time of day.</p>
<p>Because <em class="replaceable"><code>expr</code></em> is treated as a           string, be careful if you specify a non-string value with           <code class="literal">INTERVAL</code>. For example, with an interval           specifier of <code class="literal">HOUR_MINUTE</code>,           <code class="literal">6/4</code> evaluates to <code class="literal">1.5000</code>           and is treated as 1 hour, 5000 minutes:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT 6/4;</code></strong>
        -&gt; 1.5000
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1999-01-01', INTERVAL 6/4 HOUR_MINUTE);</code></strong>
        -&gt; '1999-01-04 12:20:00'</pre>
<p>To ensure interpretation of the interval value as you expect,           a <a href="http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast"><code class="literal">CAST()</code></a> operation may be           used. To treat <code class="literal">6/4</code> as 1 hour, 5 minutes,           cast it to a <code class="literal">DECIMAL</code> value with a single           fractional digit:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT CAST(6/4 AS DECIMAL(3,1));</code></strong>
        -&gt; 1.5
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1970-01-01 12:00:00',</code></strong>
    -&gt; <strong class="userinput"><code>                INTERVAL CAST(6/4 AS DECIMAL(3,1)) HOUR_MINUTE);</code></strong>
        -&gt; '1970-01-01 13:05:00'</pre>
<p>If you add to or subtract from a date value something that           contains a time part, the result is automatically converted to           a datetime value:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1999-01-01', INTERVAL 1 DAY);</code></strong>
        -&gt; '1999-01-02'
mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1999-01-01', INTERVAL 1 HOUR);</code></strong>
        -&gt; '1999-01-01 01:00:00'</pre>
<p>If you add <code class="literal">MONTH</code>,           <code class="literal">YEAR_MONTH</code>, or <code class="literal">YEAR</code> and           the resulting date has a day that is larger than the maximum           day for the new month, the day is adjusted to the maximum days           in the new month:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('1998-01-30', INTERVAL 1 MONTH);</code></strong>
        -&gt; '1998-02-28'</pre>
<p>Date arithmetic operations require complete dates and do not           work with incomplete dates such as           <code class="literal">'2006-07-00'</code> or badly malformed dates:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_ADD('2006-07-00', INTERVAL 1 DAY);</code></strong>
        -&gt; NULL
mysql&gt; <strong class="userinput"><code>SELECT '2005-03-32' + INTERVAL 1 MONTH;</code></strong>
        -&gt; NULL</pre>
</li>
<li><a name="function_date-format"></a>           <a class="indexterm" name="id2840670"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT(<em class="replaceable"><code>date</code></em>,<em class="replaceable"><code>format</code></em>)</code></a>
<p>Formats the <em class="replaceable"><code>date</code></em> value according to           the <em class="replaceable"><code>format</code></em> string.</p>
<p>The following specifiers may be used in the           <em class="replaceable"><code>format</code></em> string. The           â€œ<span class="quote"><code class="literal">%</code></span>â€ character is required           before format specifier characters.</p>
<p class="informaltable">
<table border="1">
<tr>
<td><span class="bold"><strong>Specifier</strong></span></td>
<td><span class="bold"><strong>Description</strong></span></td>
</tr>
<tr>
<td><code class="literal">%a</code></td>
<td>Abbreviated weekday name                   (<code class="literal">Sun</code>..<code class="literal">Sat</code>)</td>
</tr>
<tr>
<td><code class="literal">%b</code></td>
<td>Abbreviated month name (<code class="literal">Jan</code>..<code class="literal">Dec</code>)</td>
</tr>
<tr>
<td><code class="literal">%c</code></td>
<td>Month, numeric (<code class="literal">0</code>..<code class="literal">12</code>)</td>
</tr>
<tr>
<td><code class="literal">%D</code></td>
<td>Day of the month with English suffix (<code class="literal">0th</code>,                   <code class="literal">1st</code>, <code class="literal">2nd</code>,                   <code class="literal">3rd</code>, â€¦)</td>
</tr>
<tr>
<td><code class="literal">%d</code></td>
<td>Day of the month, numeric (<code class="literal">00</code>..<code class="literal">31</code>)</td>
</tr>
<tr>
<td><code class="literal">%e</code></td>
<td>Day of the month, numeric (<code class="literal">0</code>..<code class="literal">31</code>)</td>
</tr>
<tr>
<td><code class="literal">%f</code></td>
<td>Microseconds (<code class="literal">000000</code>..<code class="literal">999999</code>)</td>
</tr>
<tr>
<td><code class="literal">%H</code></td>
<td>Hour (<code class="literal">00</code>..<code class="literal">23</code>)</td>
</tr>
<tr>
<td><code class="literal">%h</code></td>
<td>Hour (<code class="literal">01</code>..<code class="literal">12</code>)</td>
</tr>
<tr>
<td><code class="literal">%I</code></td>
<td>Hour (<code class="literal">01</code>..<code class="literal">12</code>)</td>
</tr>
<tr>
<td><code class="literal">%i</code></td>
<td>Minutes, numeric (<code class="literal">00</code>..<code class="literal">59</code>)</td>
</tr>
<tr>
<td><code class="literal">%j</code></td>
<td>Day of year (<code class="literal">001</code>..<code class="literal">366</code>)</td>
</tr>
<tr>
<td><code class="literal">%k</code></td>
<td>Hour (<code class="literal">0</code>..<code class="literal">23</code>)</td>
</tr>
<tr>
<td><code class="literal">%l</code></td>
<td>Hour (<code class="literal">1</code>..<code class="literal">12</code>)</td>
</tr>
<tr>
<td><code class="literal">%M</code></td>
<td>Month name (<code class="literal">January</code>..<code class="literal">December</code>)</td>
</tr>
<tr>
<td><code class="literal">%m</code></td>
<td>Month, numeric (<code class="literal">00</code>..<code class="literal">12</code>)</td>
</tr>
<tr>
<td><code class="literal">%p</code></td>
<td><code class="literal">AM</code> or <code class="literal">PM</code></td>
</tr>
<tr>
<td><code class="literal">%r</code></td>
<td>Time, 12-hour (<code class="literal">hh:mm:ss</code> followed by                   <code class="literal">AM</code> or <code class="literal">PM</code>)</td>
</tr>
<tr>
<td><code class="literal">%S</code></td>
<td>Seconds (<code class="literal">00</code>..<code class="literal">59</code>)</td>
</tr>
<tr>
<td><code class="literal">%s</code></td>
<td>Seconds (<code class="literal">00</code>..<code class="literal">59</code>)</td>
</tr>
<tr>
<td><code class="literal">%T</code></td>
<td>Time, 24-hour (<code class="literal">hh:mm:ss</code>)</td>
</tr>
<tr>
<td><code class="literal">%U</code></td>
<td>Week (<code class="literal">00</code>..<code class="literal">53</code>), where Sunday is the                   first day of the week</td>
</tr>
<tr>
<td><code class="literal">%u</code></td>
<td>Week (<code class="literal">00</code>..<code class="literal">53</code>), where Monday is the                   first day of the week</td>
</tr>
<tr>
<td><code class="literal">%V</code></td>
<td>Week (<code class="literal">01</code>..<code class="literal">53</code>), where Sunday is the                   first day of the week; used with <code class="literal">%X</code></td>
</tr>
<tr>
<td><code class="literal">%v</code></td>
<td>Week (<code class="literal">01</code>..<code class="literal">53</code>), where Monday is the                   first day of the week; used with <code class="literal">%x</code></td>
</tr>
<tr>
<td><code class="literal">%W</code></td>
<td>Weekday name (<code class="literal">Sunday</code>..<code class="literal">Saturday</code>)</td>
</tr>
<tr>
<td><code class="literal">%w</code></td>
<td>Day of the week                   (<code class="literal">0</code>=Sunday..<code class="literal">6</code>=Saturday)</td>
</tr>
<tr>
<td><code class="literal">%X</code></td>
<td>Year for the week where Sunday is the first day of the week, numeric,                   four digits; used with <code class="literal">%V</code></td>
</tr>
<tr>
<td><code class="literal">%x</code></td>
<td>Year for the week, where Monday is the first day of the week, numeric,                   four digits; used with <code class="literal">%v</code></td>
</tr>
<tr>
<td><code class="literal">%Y</code></td>
<td>Year, numeric, four digits</td>
</tr>
<tr>
<td><code class="literal">%y</code></td>
<td>Year, numeric (two digits)</td>
</tr>
<tr>
<td><code class="literal">%%</code></td>
<td>A literal â€œ<span class="quote"><code class="literal">%</code></span>â€ character</td>
</tr>
<tr>
<td><code class="literal">%<em class="replaceable"><code>x</code></em></code></td>
<td><em class="replaceable"><code>x</code></em>, for any                   â€œ<span class="quote"><em class="replaceable"><code>x</code></em></span>â€ not listed                   above</td>
</tr>
</table>
<p>Ranges for the month and day specifiers begin with zero due to           the fact that MySQL allows the storing of incomplete dates           such as <code class="literal">'2004-00-00'</code>.</p>
<p>As of MySQL 5.0.25, the language used for day and month names           and abbreviations is controlled by the value of the           <code class="literal">lc_time_names</code> system variable           (<a href="http://dev.mysql.com/doc/refman/5.0/en/locale-support.html" title="8.10.Â MySQL Server Locale Support">SectionÂ 8.10, â€œMySQL Server Locale Supportâ€</a>).</p>
<p>As of MySQL 5.0.36,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> returns a string           with a character set and collation given by           <code class="literal">character_set_connection</code> and           <code class="literal">collation_connection</code> so that it can return           month and weekday names containing non-ASCII characters.           Before 5.0.36, the return value is a binary string.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');</code></strong>
        -&gt; 'Saturday October 1997'
mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');</code></strong>
        -&gt; '22:23:00'
mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00',</code></strong>
                          '%D %y %a %d %m %b %j');
        -&gt; '4th 97 Sat 04 10 Oct 277'
mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00',</code></strong>
                          '%H %k %I %r %T %S %w');
        -&gt; '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('1999-01-01', '%X %V');</code></strong>
        -&gt; '1998 52'
mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('2006-06-00', '%d');</code></strong>
        -&gt; '00'</pre>
</li>
<li><a name="function_date-sub"></a>           <a class="indexterm" name="id2841702"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-sub"><code class="literal">DATE_SUB(<em class="replaceable"><code>date</code></em>,INTERVAL           <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em>)</code></a>
<p>See the description for           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a>.</li>
<li><a name="function_day"></a>           <a class="indexterm" name="id2841774"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_day"><code class="literal">DAY(<em class="replaceable"><code>date</code></em>)</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_day"><code class="literal">DAY()</code></a> is a synonym for           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofmonth"><code class="literal">DAYOFMONTH()</code></a>.</li>
<li><a name="function_dayname"></a>           <a class="indexterm" name="id2841845"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayname"><code class="literal">DAYNAME(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the name of the weekday for           <em class="replaceable"><code>date</code></em>. As of MySQL 5.0.25, the           language used for the name is controlled by the value of the           <code class="literal">lc_time_names</code> system variable           (<a href="http://dev.mysql.com/doc/refman/5.0/en/locale-support.html" title="8.10.Â MySQL Server Locale Support">SectionÂ 8.10, â€œMySQL Server Locale Supportâ€</a>).</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DAYNAME('1998-02-05');</code></strong>
        -&gt; 'Thursday'</pre>
</li>
<li><a name="function_dayofmonth"></a>           <a class="indexterm" name="id2841937"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofmonth"><code class="literal">DAYOFMONTH(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the day of the month for           <em class="replaceable"><code>date</code></em>, in the range           <code class="literal">1</code> to <code class="literal">31</code>, or           <code class="literal">0</code> for dates such as           <code class="literal">'0000-00-00'</code> or           <code class="literal">'2008-00-00'</code> that have a zero day part.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DAYOFMONTH('1998-02-03');</code></strong>
        -&gt; 3</pre>
</li>
<li><a name="function_dayofweek"></a>           <a class="indexterm" name="id2842046"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofweek"><code class="literal">DAYOFWEEK(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the weekday index for <em class="replaceable"><code>date</code></em>           (<code class="literal">1</code> = Sunday, <code class="literal">2</code> = Monday,           â€¦, <code class="literal">7</code> = Saturday). These index values           correspond to the ODBC standard.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DAYOFWEEK('1998-02-03');</code></strong>
        -&gt; 3</pre>
</li>
<li><a name="function_dayofyear"></a>           <a class="indexterm" name="id2842144"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofyear"><code class="literal">DAYOFYEAR(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the day of the year for           <em class="replaceable"><code>date</code></em>, in the range           <code class="literal">1</code> to <code class="literal">366</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DAYOFYEAR('1998-02-03');</code></strong>
        -&gt; 34</pre>
</li>
<li><a name="function_extract"></a>           <a class="indexterm" name="id2842234"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_extract"><code class="literal">EXTRACT(<em class="replaceable"><code>unit</code></em>           FROM <em class="replaceable"><code>date</code></em>)</code></a>
<p>The <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_extract"><code class="literal">EXTRACT()</code></a> function uses           the same kinds of unit specifiers as           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a> or           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a>, but extracts parts           from the date rather than performing date arithmetic.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT EXTRACT(YEAR FROM '1999-07-02');</code></strong>
       -&gt; 1999
mysql&gt; <strong class="userinput"><code>SELECT EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03');</code></strong>
       -&gt; 199907
mysql&gt; <strong class="userinput"><code>SELECT EXTRACT(DAY_MINUTE FROM '1999-07-02 01:02:03');</code></strong>
       -&gt; 20102
mysql&gt; <strong class="userinput"><code>SELECT EXTRACT(MICROSECOND</code></strong>
    -&gt;                <strong class="userinput"><code>FROM '2003-01-02 10:30:00.000123');</code></strong>
        -&gt; 123</pre>
</li>
<li><a name="function_from-days"></a>           <a class="indexterm" name="id2842366"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-days"><code class="literal">FROM_DAYS(<em class="replaceable"><code>N</code></em>)</code></a>
<p>Given a day number <em class="replaceable"><code>N</code></em>, returns a           <code class="literal">DATE</code> value.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT FROM_DAYS(729669);</code></strong>
        -&gt; '1997-10-07'</pre>
<p>Use <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-days"><code class="literal">FROM_DAYS()</code></a> with caution           on old dates. It is not intended for use with values that           precede the advent of the Gregorian calendar (1582). See           <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-calendar.html" title="10.7.Â What Calendar Is Used By MySQL?">SectionÂ 10.7, â€œWhat Calendar Is Used By MySQL?â€</a>.</li>
<li><a name="function_from-unixtime"></a>           <a class="indexterm" name="id2842469"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME(<em class="replaceable"><code>unix_timestamp</code></em>)</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME(<em class="replaceable"><code>unix_timestamp</code></em>,<em class="replaceable"><code>format</code></em>)</code></a>
<p>Returns a representation of the           <em class="replaceable"><code>unix_timestamp</code></em> argument as a value           in <code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or           <code class="literal">YYYYMMDDHHMMSS.uuuuuu</code> format, depending on           whether the function is used in a string or numeric context.           The value is expressed in the current time zone.           <em class="replaceable"><code>unix_timestamp</code></em> is an internal           timestamp value such as is produced by the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> function.</p>
<p>If <em class="replaceable"><code>format</code></em> is given, the result is           formatted according to the <em class="replaceable"><code>format</code></em>           string, which is used the same way as listed in the entry for           the <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> function.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT FROM_UNIXTIME(1196440219);</code></strong>
        -&gt; '2007-11-30 10:30:19'
mysql&gt; <strong class="userinput"><code>SELECT FROM_UNIXTIME(1196440219) + 0;</code></strong>
        -&gt; 20071130103019.000000
mysql&gt; <strong class="userinput"><code>SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),</code></strong>
    -&gt;                      <strong class="userinput"><code>'%Y %D %M %h:%i:%s %x');</code></strong>
        -&gt; '2007 30th November 10:30:59 2007'</pre>
<p>Note: If you use           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a> to convert           between <code class="literal">TIMESTAMP</code> values and Unix timestamp           values, the conversion is lossy because the mapping is not           one-to-one in both directions. For details, see the           description of the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> function.</li>
<li><a name="function_get-format"></a>           <a class="indexterm" name="id2842673"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATE|TIME|DATETIME,           'EUR'|'USA'|'JIS'|'ISO'|'INTERNAL')</code></a>
<p>Returns a format string. This function is useful in           combination with the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> and the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date"><code class="literal">STR_TO_DATE()</code></a> functions.</p>
<p>The possible values for the first and second arguments result           in several possible format strings (for the specifiers used,           see the table in the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> function           description). ISO format refers to ISO 9075, not ISO 8601.</p>
<p class="informaltable">
<table border="1">
<tr>
<td><span class="bold"><strong>Function Call</strong></span></td>
<td><span class="bold"><strong>Result</strong></span></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATE,'USA')</code></a></td>
<td><code class="literal">'%m.%d.%Y'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATE,'JIS')</code></a></td>
<td><code class="literal">'%Y-%m-%d'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATE,'ISO')</code></a></td>
<td><code class="literal">'%Y-%m-%d'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATE,'EUR')</code></a></td>
<td><code class="literal">'%d.%m.%Y'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATE,'INTERNAL')</code></a></td>
<td><code class="literal">'%Y%m%d'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATETIME,'USA')</code></a></td>
<td><code class="literal">'%Y-%m-%d %H.%i.%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATETIME,'JIS')</code></a></td>
<td><code class="literal">'%Y-%m-%d %H:%i:%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATETIME,'ISO')</code></a></td>
<td><code class="literal">'%Y-%m-%d %H:%i:%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATETIME,'EUR')</code></a></td>
<td><code class="literal">'%Y-%m-%d %H.%i.%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(DATETIME,'INTERNAL')</code></a></td>
<td><code class="literal">'%Y%m%d%H%i%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(TIME,'USA')</code></a></td>
<td><code class="literal">'%h:%i:%s %p'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(TIME,'JIS')</code></a></td>
<td><code class="literal">'%H:%i:%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(TIME,'ISO')</code></a></td>
<td><code class="literal">'%H:%i:%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(TIME,'EUR')</code></a></td>
<td><code class="literal">'%H.%i.%s'</code></td>
</tr>
<tr>
<td><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT(TIME,'INTERNAL')</code></a></td>
<td><code class="literal">'%H%i%s'</code></td>
</tr>
</table>
<p><code class="literal">TIMESTAMP</code> can also be used as the first           argument to <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format"><code class="literal">GET_FORMAT()</code></a>, in           which case the function returns the same values as for           <code class="literal">DATETIME</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_FORMAT('2003-10-03',GET_FORMAT(DATE,'EUR'));</code></strong>
        -&gt; '03.10.2003'
mysql&gt; <strong class="userinput"><code>SELECT STR_TO_DATE('10.31.2003',GET_FORMAT(DATE,'USA'));</code></strong>
        -&gt; '2003-10-31'</pre>
</li>
<li><a name="function_hour"></a>           <a class="indexterm" name="id2843139"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_hour"><code class="literal">HOUR(<em class="replaceable"><code>time</code></em>)</code></a>
<p>Returns the hour for <em class="replaceable"><code>time</code></em>. The           range of the return value is <code class="literal">0</code> to           <code class="literal">23</code> for time-of-day values. However, the           range of <code class="literal">TIME</code> values actually is much           larger, so <code class="literal">HOUR</code> can return values greater           than <code class="literal">23</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT HOUR('10:05:03');</code></strong>
        -&gt; 10
mysql&gt; <strong class="userinput"><code>SELECT HOUR('272:59:59');</code></strong>
        -&gt; 272</pre>
</li>
<li><a name="function_last-day"></a>           <a class="indexterm" name="id2843256"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_last-day"><code class="literal">LAST_DAY(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Takes a date or datetime value and returns the corresponding           value for the last day of the month. Returns           <code class="literal">NULL</code> if the argument is invalid.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT LAST_DAY('2003-02-05');</code></strong>
        -&gt; '2003-02-28'
mysql&gt; <strong class="userinput"><code>SELECT LAST_DAY('2004-02-05');</code></strong>
        -&gt; '2004-02-29'
mysql&gt; <strong class="userinput"><code>SELECT LAST_DAY('2004-01-01 01:01:01');</code></strong>
        -&gt; '2004-01-31'
mysql&gt; <strong class="userinput"><code>SELECT LAST_DAY('2003-03-32');</code></strong>
        -&gt; NULL</pre>
</li>
<li><a name="function_localtime"></a>           <a class="indexterm" name="id2843362"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtime"><code class="literal">LOCALTIME</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtime"><code class="literal">LOCALTIME()</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtime"><code class="literal">LOCALTIME</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtime"><code class="literal">LOCALTIME()</code></a> are synonyms for           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a>.</li>
<li><a name="function_localtimestamp"></a>           <a class="indexterm" name="id2843444"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP()</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP()</code></a> are synonyms           for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a>.</li>
<li><a name="function_makedate"></a>           <a class="indexterm" name="id2843528"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_makedate"><code class="literal">MAKEDATE(<em class="replaceable"><code>year</code></em>,<em class="replaceable"><code>dayofyear</code></em>)</code></a>
<p>Returns a date, given year and day-of-year values.           <em class="replaceable"><code>dayofyear</code></em> must be greater than 0 or           the result is <code class="literal">NULL</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT MAKEDATE(2001,31), MAKEDATE(2001,32);</code></strong>
        -&gt; '2001-01-31', '2001-02-01'
mysql&gt; <strong class="userinput"><code>SELECT MAKEDATE(2001,365), MAKEDATE(2004,365);</code></strong>
        -&gt; '2001-12-31', '2004-12-30'
mysql&gt; <strong class="userinput"><code>SELECT MAKEDATE(2001,0);</code></strong>
        -&gt; NULL</pre>
</li>
<li><a name="function_maketime"></a>           <a class="indexterm" name="id2843633"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_maketime"><code class="literal">MAKETIME(<em class="replaceable"><code>hour</code></em>,<em class="replaceable"><code>minute</code></em>,<em class="replaceable"><code>second</code></em>)</code></a>
<p>Returns a time value calculated from the           <em class="replaceable"><code>hour</code></em>,           <em class="replaceable"><code>minute</code></em>, and           <em class="replaceable"><code>second</code></em> arguments.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT MAKETIME(12,15,30);</code></strong>
        -&gt; '12:15:30'</pre>
</li>
<li><a name="function_microsecond"></a>           <a class="indexterm" name="id2843727"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_microsecond"><code class="literal">MICROSECOND(<em class="replaceable"><code>expr</code></em>)</code></a>
<p>Returns the microseconds from the time or datetime expression           <em class="replaceable"><code>expr</code></em> as a number in the range from           <code class="literal">0</code> to <code class="literal">999999</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT MICROSECOND('12:00:00.123456');</code></strong>
        -&gt; 123456
mysql&gt; <strong class="userinput"><code>SELECT MICROSECOND('1997-12-31 23:59:59.000010');</code></strong>
        -&gt; 10</pre>
</li>
<li><a name="function_minute"></a>           <a class="indexterm" name="id2843826"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_minute"><code class="literal">MINUTE(<em class="replaceable"><code>time</code></em>)</code></a>
<p>Returns the minute for <em class="replaceable"><code>time</code></em>, in the           range <code class="literal">0</code> to <code class="literal">59</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT MINUTE('98-02-03 10:05:03');</code></strong>
        -&gt; 5</pre>
</li>
<li><a name="function_month"></a>           <a class="indexterm" name="id2843916"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_month"><code class="literal">MONTH(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the month for <em class="replaceable"><code>date</code></em>, in the           range <code class="literal">1</code> to <code class="literal">12</code> for           January to December, or <code class="literal">0</code> for dates such as           <code class="literal">'0000-00-00'</code> or           <code class="literal">'2008-00-00'</code> that have a zero month part.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT MONTH('1998-02-03');</code></strong>
        -&gt; 2</pre>
</li>
<li><a name="function_monthname"></a>           <a class="indexterm" name="id2844025"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_monthname"><code class="literal">MONTHNAME(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the full name of the month for           <em class="replaceable"><code>date</code></em>. As of MySQL 5.0.25, the           language used for the name is controlled by the value of the           <code class="literal">lc_time_names</code> system variable           (<a href="http://dev.mysql.com/doc/refman/5.0/en/locale-support.html" title="8.10.Â MySQL Server Locale Support">SectionÂ 8.10, â€œMySQL Server Locale Supportâ€</a>).</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT MONTHNAME('1998-02-05');</code></strong>
        -&gt; 'February'</pre>
</li>
<li><a name="function_now"></a>           <a class="indexterm" name="id2844116"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a>
<p>Returns the current date and time as a value in           <code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or           <code class="literal">YYYYMMDDHHMMSS.uuuuuu</code> format, depending on           whether the function is used in a string or numeric context.           The value is expressed in the current time zone.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT NOW();</code></strong>
        -&gt; '2007-12-15 23:50:26'
mysql&gt; <strong class="userinput"><code>SELECT NOW() + 0;</code></strong>
        -&gt; 20071215235026.000000</pre>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a> returns a constant time           that indicates the time at which the statement began to           execute. (Within a stored routine or trigger,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a> returns the time at           which the routine or triggering statement began to execute.)           This differs from the behavior for           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a>, which returns the           exact time at which it executes as of MySQL 5.0.13.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT NOW(), SLEEP(2), NOW();</code></strong>
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql&gt; <strong class="userinput"><code>SELECT SYSDATE(), SLEEP(2), SYSDATE();</code></strong>
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+</pre>
<p>See the description for           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a> for additional           information about the differences between the two functions.</li>
<li><a name="function_period-add"></a>           <a class="indexterm" name="id2844289"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_period-add"><code class="literal">PERIOD_ADD(<em class="replaceable"><code>P</code></em>,<em class="replaceable"><code>N</code></em>)</code></a>
<p>Adds <em class="replaceable"><code>N</code></em> months to period           <em class="replaceable"><code>P</code></em> (in the format           <code class="literal">YYMM</code> or <code class="literal">YYYYMM</code>). Returns           a value in the format <code class="literal">YYYYMM</code>. Note that the           period argument <em class="replaceable"><code>P</code></em> is           <span class="emphasis"><em>not</em></span> a date value.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT PERIOD_ADD(9801,2);</code></strong>
        -&gt; 199803</pre>
</li>
<li><a name="function_period-diff"></a>           <a class="indexterm" name="id2844400"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_period-diff"><code class="literal">PERIOD_DIFF(<em class="replaceable"><code>P1</code></em>,<em class="replaceable"><code>P2</code></em>)</code></a>
<p>Returns the number of months between periods           <em class="replaceable"><code>P1</code></em> and           <em class="replaceable"><code>P2</code></em>. <em class="replaceable"><code>P1</code></em>           and <em class="replaceable"><code>P2</code></em> should be in the format           <code class="literal">YYMM</code> or <code class="literal">YYYYMM</code>. Note           that the period arguments <em class="replaceable"><code>P1</code></em> and           <em class="replaceable"><code>P2</code></em> are <span class="emphasis"><em>not</em></span>           date values.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT PERIOD_DIFF(9802,199703);</code></strong>
        -&gt; 11</pre>
</li>
<li><a name="function_quarter"></a>           <a class="indexterm" name="id2844516"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_quarter"><code class="literal">QUARTER(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the quarter of the year for           <em class="replaceable"><code>date</code></em>, in the range           <code class="literal">1</code> to <code class="literal">4</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT QUARTER('98-04-01');</code></strong>
        -&gt; 2</pre>
</li>
<li><a name="function_second"></a>           <a class="indexterm" name="id2844606"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_second"><code class="literal">SECOND(<em class="replaceable"><code>time</code></em>)</code></a>
<p>Returns the second for <em class="replaceable"><code>time</code></em>, in the           range <code class="literal">0</code> to <code class="literal">59</code>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT SECOND('10:05:03');</code></strong>
        -&gt; 3</pre>
</li>
<li><a name="function_sec-to-time"></a>           <a class="indexterm" name="id2844696"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sec-to-time"><code class="literal">SEC_TO_TIME(<em class="replaceable"><code>seconds</code></em>)</code></a>
<p>Returns the <em class="replaceable"><code>seconds</code></em> argument,           converted to hours, minutes, and seconds, as a           <code class="literal">TIME</code> value. The range of the result is           constrained to that of the <code class="literal">TIME</code> data type.           A warning occurs if the argument corresponds to a value           outside that range.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT SEC_TO_TIME(2378);</code></strong>
        -&gt; '00:39:38'
mysql&gt; <strong class="userinput"><code>SELECT SEC_TO_TIME(2378) + 0;</code></strong>
        -&gt; 3938</pre>
</li>
<li><a name="function_str-to-date"></a>           <a class="indexterm" name="id2844798"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date"><code class="literal">STR_TO_DATE(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>format</code></em>)</code></a>
<p>This is the inverse of the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> function. It           takes a string <em class="replaceable"><code>str</code></em> and a format           string <em class="replaceable"><code>format</code></em>.           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date"><code class="literal">STR_TO_DATE()</code></a> returns a           <code class="literal">DATETIME</code> value if the format string           contains both date and time parts, or a           <code class="literal">DATE</code> or <code class="literal">TIME</code> value if           the string contains only date or time parts.</p>
<p>The date, time, or datetime values contained in           <em class="replaceable"><code>str</code></em> should be given in the format           indicated by <em class="replaceable"><code>format</code></em>. For the           specifiers that can be used in           <em class="replaceable"><code>format</code></em>, see the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> function           description. If <em class="replaceable"><code>str</code></em> contains an           illegal date, time, or datetime value,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date"><code class="literal">STR_TO_DATE()</code></a> returns           <code class="literal">NULL</code>. Starting from MySQL 5.0.3, an illegal           value also produces a warning.</p>
<p>Range checking on the parts of date values is as described in           <a href="http://dev.mysql.com/doc/refman/5.0/en/datetime.html" title="9.3.1.Â The DATETIME, DATE, and         TIMESTAMP Types">SectionÂ 9.3.1, â€œThe <code class="literal">DATETIME</code>, <code class="literal">DATE</code>, and         <code class="literal">TIMESTAMP</code> Typesâ€</a>. This means, for example, that           â€œ<span class="quote">zero</span>â€ dates or dates with part values of 0 are           allowed unless the SQL mode is set to disallow such values.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');</code></strong>
        -&gt; '0000-00-00'
mysql&gt; <strong class="userinput"><code>SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');</code></strong>
        -&gt; '2004-04-31'</pre>
<p class="note" style="margin-left: 0.5in; margin-right: 0.5in">
<h3 class="title">Note</h3>
<p>You cannot use format <code class="literal">"%X%V"</code> to convert a             year-week string to a date because the combination of a year             and week does not uniquely identify a year and month if the             week crosses a month boundary. To convert a year-week to a             date, then you should also specify the weekday:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT STR_TO_DATE('200442 Monday', '%X%V %W');</code></strong>
        -&gt; '2004-10-18'</pre>
</li>
<li><a name="function_subdate"></a>           <a class="indexterm" name="id2845043"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subdate"><code class="literal">SUBDATE(<em class="replaceable"><code>date</code></em>,INTERVAL           <em class="replaceable"><code>expr</code></em>           <em class="replaceable"><code>unit</code></em>)</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subdate"><code class="literal">SUBDATE(<em class="replaceable"><code>expr</code></em>,<em class="replaceable"><code>days</code></em>)</code></a>
<p>When invoked with the <code class="literal">INTERVAL</code> form of the           second argument, <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subdate"><code class="literal">SUBDATE()</code></a> is           a synonym for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a>. For           information on the <code class="literal">INTERVAL</code>           <em class="replaceable"><code>unit</code></em> argument, see the discussion           for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);</code></strong>
        -&gt; '1997-12-02'
mysql&gt; <strong class="userinput"><code>SELECT SUBDATE('1998-01-02', INTERVAL 31 DAY);</code></strong>
        -&gt; '1997-12-02'</pre>
<p>The second form allows the use of an integer value for           <em class="replaceable"><code>days</code></em>. In such cases, it is           interpreted as the number of days to be subtracted from the           date or datetime expression <em class="replaceable"><code>expr</code></em>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT SUBDATE('1998-01-02 12:00:00', 31);</code></strong>
        -&gt; '1997-12-02 12:00:00'</pre>
</li>
<li><a name="function_subtime"></a>           <a class="indexterm" name="id2845211"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subtime"><code class="literal">SUBTIME(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_subtime"><code class="literal">SUBTIME()</code></a> returns           <em class="replaceable"><code>expr1</code></em> â€“           <em class="replaceable"><code>expr2</code></em> expressed as a value in the           same format as <em class="replaceable"><code>expr1</code></em>.           <em class="replaceable"><code>expr1</code></em> is a time or datetime           expression, and <em class="replaceable"><code>expr2</code></em> is a time           expression.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT SUBTIME('1997-12-31 23:59:59.999999','1 1:1:1.000002');</code></strong>
        -&gt; '1997-12-30 22:58:58.999997'
mysql&gt; <strong class="userinput"><code>SELECT SUBTIME('01:00:00.999999', '02:00:00.999998');</code></strong>
        -&gt; '-00:59:59.999999'</pre>
</li>
<li><a name="function_sysdate"></a>           <a class="indexterm" name="id2845333"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a>
<p>Returns the current date and time as a value in           <code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or           <code class="literal">YYYYMMDDHHMMSS.uuuuuu</code> format, depending on           whether the function is used in a string or numeric context.</p>
<p>As of MySQL 5.0.13, <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a>           returns the time at which it executes. This differs from the           behavior for <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a>, which           returns a constant time that indicates the time at which the           statement began to execute. (Within a stored routine or           trigger, <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a> returns the           time at which the routine or triggering statement began to           execute.)</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT NOW(), SLEEP(2), NOW();</code></strong>
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql&gt; <strong class="userinput"><code>SELECT SYSDATE(), SLEEP(2), SYSDATE();</code></strong>
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+</pre>
<p>In addition, the <code class="literal">SET TIMESTAMP</code> statement           affects the value returned by           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a> but not by           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a>. This means that           timestamp settings in the binary log have no effect on           invocations of <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a>.</p>
<p>Because <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a> can return           different values even within the same statement, and is not           affected by <code class="literal">SET TIMESTAMP</code>, it is           non-deterministic and therefore unsafe for replication. If           that is a problem, you can start the server with the           <code class="option">--sysdate-is-now</code> option to cause           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a> to be an alias for           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now"><code class="literal">NOW()</code></a>. The non-deterministic           nature of <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a> also means           that indexes cannot be used for evaluating expressions that           refer to it.</li>
<li><a name="function_time"></a>           <a class="indexterm" name="id2845557"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time"><code class="literal">TIME(<em class="replaceable"><code>expr</code></em>)</code></a>
<p>Extracts the time part of the time or datetime expression           <em class="replaceable"><code>expr</code></em> and returns it as a string.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIME('2003-12-31 01:02:03');</code></strong>
        -&gt; '01:02:03'
mysql&gt; <strong class="userinput"><code>SELECT TIME('2003-12-31 01:02:03.000123');</code></strong>
        -&gt; '01:02:03.000123'</pre>
</li>
<li><a name="function_timediff"></a>           <a class="indexterm" name="id2845645"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timediff"><code class="literal">TIMEDIFF(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code></a>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timediff"><code class="literal">TIMEDIFF()</code></a> returns           <em class="replaceable"><code>expr1</code></em> â€“           <em class="replaceable"><code>expr2</code></em> expressed as a time value.           <em class="replaceable"><code>expr1</code></em> and           <em class="replaceable"><code>expr2</code></em> are time or date-and-time           expressions, but both must be of the same type.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIMEDIFF('2000:01:01 00:00:00',</code></strong>
    -&gt;                 <strong class="userinput"><code>'2000:01:01 00:00:00.000001');</code></strong>
        -&gt; '-00:00:00.000001'
mysql&gt; <strong class="userinput"><code>SELECT TIMEDIFF('1997-12-31 23:59:59.000001',</code></strong>
    -&gt;                 <strong class="userinput"><code>'1997-12-30 01:01:01.000002');</code></strong>
        -&gt; '46:58:57.999999'</pre>
</li>
<li><a name="function_timestamp"></a>           <a class="indexterm" name="id2845784"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestamp"><code class="literal">TIMESTAMP(<em class="replaceable"><code>expr</code></em>)</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestamp"><code class="literal">TIMESTAMP(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code></a>
<p>With a single argument, this function returns the date or           datetime expression <em class="replaceable"><code>expr</code></em> as a           datetime value. With two arguments, it adds the time           expression <em class="replaceable"><code>expr2</code></em> to the date or           datetime expression <em class="replaceable"><code>expr1</code></em> and           returns the result as a datetime value.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIMESTAMP('2003-12-31');</code></strong>
        -&gt; '2003-12-31 00:00:00'
mysql&gt; <strong class="userinput"><code>SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');</code></strong>
        -&gt; '2004-01-01 00:00:00'</pre>
</li>
<li><a name="function_timestampadd"></a>           <a class="indexterm" name="id2845897"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampadd"><code class="literal">TIMESTAMPADD(<em class="replaceable"><code>unit</code></em>,<em class="replaceable"><code>interval</code></em>,<em class="replaceable"><code>datetime_expr</code></em>)</code></a>
<p>Adds the integer expression           <em class="replaceable"><code>interval</code></em> to the date or datetime           expression <em class="replaceable"><code>datetime_expr</code></em>. The unit           for <em class="replaceable"><code>interval</code></em> is given by the           <em class="replaceable"><code>unit</code></em> argument, which should be one           of the following values: <code class="literal">FRAC_SECOND</code>           (microseconds), <code class="literal">SECOND</code>,           <code class="literal">MINUTE</code>, <code class="literal">HOUR</code>,           <code class="literal">DAY</code>, <code class="literal">WEEK</code>,           <code class="literal">MONTH</code>, <code class="literal">QUARTER</code>, or           <code class="literal">YEAR</code>.</p>
<p>The <em class="replaceable"><code>unit</code></em> value may be specified           using one of keywords as shown, or with a prefix of           <code class="literal">SQL_TSI_</code>. For example,           <code class="literal">DAY</code> and <code class="literal">SQL_TSI_DAY</code> both           are legal.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');</code></strong>
        -&gt; '2003-01-02 00:01:00'
mysql&gt; <strong class="userinput"><code>SELECT TIMESTAMPADD(WEEK,1,'2003-01-02');</code></strong>
        -&gt; '2003-01-09'</pre>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampadd"><code class="literal">TIMESTAMPADD()</code></a> is available as           of MySQL 5.0.0.</li>
<li><a name="function_timestampdiff"></a>           <a class="indexterm" name="id2846098"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff"><code class="literal">TIMESTAMPDIFF(<em class="replaceable"><code>unit</code></em>,<em class="replaceable"><code>datetime_expr1</code></em>,<em class="replaceable"><code>datetime_expr2</code></em>)</code></a>
<p>Returns the integer difference between the date or datetime           expressions <em class="replaceable"><code>datetime_expr1</code></em> and           <em class="replaceable"><code>datetime_expr2</code></em>. The unit for the           result is given by the <em class="replaceable"><code>unit</code></em>           argument. The legal values for <em class="replaceable"><code>unit</code></em>           are the same as those listed in the description of the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampadd"><code class="literal">TIMESTAMPADD()</code></a> function.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');</code></strong>
        -&gt; 3
mysql&gt; <strong class="userinput"><code>SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');</code></strong>
        -&gt; -1</pre>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff"><code class="literal">TIMESTAMPDIFF()</code></a> is available           as of MySQL 5.0.0.</li>
<li><a name="function_time-format"></a>           <a class="indexterm" name="id2846225"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time-format"><code class="literal">TIME_FORMAT(<em class="replaceable"><code>time</code></em>,<em class="replaceable"><code>format</code></em>)</code></a>
<p>This is used like the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a> function, but           the <em class="replaceable"><code>format</code></em> string may contain           format specifiers only for hours, minutes, and seconds. Other           specifiers produce a <code class="literal">NULL</code> value or           <code class="literal">0</code>.</p>
<p>If the <em class="replaceable"><code>time</code></em> value contains an hour           part that is greater than <code class="literal">23</code>, the           <code class="literal">%H</code> and <code class="literal">%k</code> hour format           specifiers produce a value larger than the usual range of           <code class="literal">0..23</code>. The other hour format specifiers           produce the hour value modulo 12.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');</code></strong>
        -&gt; '100 100 04 04 4'</pre>
</li>
<li><a name="function_time-to-sec"></a>           <a class="indexterm" name="id2846365"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time-to-sec"><code class="literal">TIME_TO_SEC(<em class="replaceable"><code>time</code></em>)</code></a>
<p>Returns the <em class="replaceable"><code>time</code></em> argument,           converted to seconds.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TIME_TO_SEC('22:23:00');</code></strong>
        -&gt; 80580
mysql&gt; <strong class="userinput"><code>SELECT TIME_TO_SEC('00:39:38');</code></strong>
        -&gt; 2378</pre>
</li>
<li><a name="function_to-days"></a>           <a class="indexterm" name="id2846451"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_to-days"><code class="literal">TO_DAYS(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Given a date <em class="replaceable"><code>date</code></em>, returns a day           number (the number of days since year 0).</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TO_DAYS(950501);</code></strong>
        -&gt; 728779
mysql&gt; <strong class="userinput"><code>SELECT TO_DAYS('1997-10-07');</code></strong>
        -&gt; 729669</pre>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_to-days"><code class="literal">TO_DAYS()</code></a> is not intended for           use with values that precede the advent of the Gregorian           calendar (1582), because it does not take into account the           days that were lost when the calendar was changed. For dates           before 1582 (and possibly a later year in other locales),           results from this function are not reliable. See           <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-calendar.html" title="10.7.Â What Calendar Is Used By MySQL?">SectionÂ 10.7, â€œWhat Calendar Is Used By MySQL?â€</a>, for details.</p>
<p>Remember that MySQL converts two-digit year values in dates to           four-digit form using the rules in           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html" title="9.3.Â Date and Time Types">SectionÂ 9.3, â€œDate and Time Typesâ€</a>. For example,           <code class="literal">'1997-10-07'</code> and           <code class="literal">'97-10-07'</code> are seen as identical dates:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TO_DAYS('1997-10-07'), TO_DAYS('97-10-07');</code></strong>
        -&gt; 729669, 729669</pre>
</li>
<li><a name="function_unix-timestamp"></a>           <a class="indexterm" name="id2846596"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP(<em class="replaceable"><code>date</code></em>)</code></a>
<p>If called with no argument, returns a Unix timestamp (seconds           since <code class="literal">'1970-01-01 00:00:00'</code> UTC) as an           unsigned integer. If           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> is called           with a <em class="replaceable"><code>date</code></em> argument, it returns           the value of the argument as seconds since           <code class="literal">'1970-01-01 00:00:00'</code> UTC.           <em class="replaceable"><code>date</code></em> may be a           <code class="literal">DATE</code> string, a <code class="literal">DATETIME</code>           string, a <code class="literal">TIMESTAMP</code>, or a number in the           format <code class="literal">YYMMDD</code> or           <code class="literal">YYYYMMDD</code>. The server interprets           <em class="replaceable"><code>date</code></em> as a value in the current time           zone and converts it to an internal value in UTC. Clients can           set their time zone as described in           <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" title="8.9.Â MySQL Server Time Zone Support">SectionÂ 8.9, â€œMySQL Server Time Zone Supportâ€</a>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT UNIX_TIMESTAMP();</code></strong>
        -&gt; 1196440210
mysql&gt; <strong class="userinput"><code>SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');</code></strong>
        -&gt; 1196440219</pre>
<p>When <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> is used           on a <code class="literal">TIMESTAMP</code> column, the function returns           the internal timestamp value directly, with no implicit           â€œ<span class="quote">string-to-Unix-timestamp</span>â€ conversion. If you           pass an out-of-range date to           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a>, it returns           <code class="literal">0</code>.</p>
<p>Note: If you use           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> and           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a> to convert           between <code class="literal">TIMESTAMP</code> values and Unix timestamp           values, the conversion is lossy because the mapping is not           one-to-one in both directions. For example, due to conventions           for local time zone changes, it is possible for two           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> to map two           <code class="literal">TIMESTAMP</code> values to the same Unix timestamp           value. <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a> will           map that value back to only one of the original           <code class="literal">TIMESTAMP</code> values. Here is an example, using           <code class="literal">TIMESTAMP</code> values in the           <code class="literal">CET</code> time zone:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT UNIX_TIMESTAMP('2005-03-27 03:00:00');</code></strong>
+---------------------------------------+
| UNIX_TIMESTAMP('2005-03-27 03:00:00') |
+---------------------------------------+
|                            1111885200 |
+---------------------------------------+
mysql&gt; <strong class="userinput"><code>SELECT UNIX_TIMESTAMP('2005-03-27 02:00:00');</code></strong>
+---------------------------------------+
| UNIX_TIMESTAMP('2005-03-27 02:00:00') |
+---------------------------------------+
|                            1111885200 |
+---------------------------------------+
mysql&gt; <strong class="userinput"><code>SELECT FROM_UNIXTIME(1111885200);</code></strong>
+---------------------------+
| FROM_UNIXTIME(1111885200) |
+---------------------------+
| 2005-03-27 03:00:00       |
+---------------------------+</pre>
<p>If you want to subtract           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a> columns, you           might want to cast the result to signed integers. See           <a href="http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html" title="10.9.Â Cast Functions and Operators">SectionÂ 10.9, â€œCast Functions and Operatorsâ€</a>.</li>
<li><a name="function_utc-date"></a>           <a class="indexterm" name="id2846929"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-date"><code class="literal">UTC_DATE</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-date"><code class="literal">UTC_DATE()</code></a>
<p>Returns the current UTC date as a value in           <code class="literal">'YYYY-MM-DD'</code> or <code class="literal">YYYYMMDD</code>           format, depending on whether the function is used in a string           or numeric context.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT UTC_DATE(), UTC_DATE() + 0;</code></strong>
        -&gt; '2003-08-14', 20030814</pre>
</li>
<li><a name="function_utc-time"></a>           <a class="indexterm" name="id2847022"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-time"><code class="literal">UTC_TIME</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-time"><code class="literal">UTC_TIME()</code></a>
<p>Returns the current UTC time as a value in           <code class="literal">'HH:MM:SS'</code> or           <code class="literal">HHMMSS.uuuuuu</code> format, depending on whether           the function is used in a string or numeric context.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT UTC_TIME(), UTC_TIME() + 0;</code></strong>
        -&gt; '18:07:53', 180753.000000</pre>
</li>
<li><a name="function_utc-timestamp"></a>           <a class="indexterm" name="id2847116"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-timestamp"><code class="literal">UTC_TIMESTAMP</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_utc-timestamp"><code class="literal">UTC_TIMESTAMP()</code></a>
<p>Returns the current UTC date and time as a value in           <code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or           <code class="literal">YYYYMMDDHHMMSS.uuuuuu</code> format, depending on           whether the function is used in a string or numeric context.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;</code></strong>
        -&gt; '2003-08-14 18:08:04', 20030814180804.000000</pre>
</li>
<li><a name="function_week"></a>           <a class="indexterm" name="id2847212"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK(<em class="replaceable"><code>date</code></em>[,<em class="replaceable"><code>mode</code></em>])</code></a>
<p>This function returns the week number for           <em class="replaceable"><code>date</code></em>. The two-argument form of           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a> allows you to specify           whether the week starts on Sunday or Monday and whether the           return value should be in the range from <code class="literal">0</code>           to <code class="literal">53</code> or from <code class="literal">1</code> to           <code class="literal">53</code>. If the <em class="replaceable"><code>mode</code></em>           argument is omitted, the value of the           <code class="literal">default_week_format</code> system variable is           used. See <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html" title="5.1.3.Â System Variables">SectionÂ 5.1.3, â€œSystem Variablesâ€</a>.</p>
<p>The following table describes how the           <em class="replaceable"><code>mode</code></em> argument works.</p>
<p class="informaltable">
<table border="1">
<tr>
<td></td>
<td><span class="bold"><strong>First day</strong></span></td>
<td></td>
<td></td>
</tr>
<tr>
<td><span class="bold"><strong>Mode</strong></span></td>
<td><span class="bold"><strong>of week</strong></span></td>
<td><span class="bold"><strong>Range</strong></span></td>
<td><span class="bold"><strong>Week 1 is the first week â€¦</strong></span></td>
</tr>
<tr>
<td>0</td>
<td>Sunday</td>
<td>0-53</td>
<td>with a Sunday in this year</td>
</tr>
<tr>
<td>1</td>
<td>Monday</td>
<td>0-53</td>
<td>with more than 3 days this year</td>
</tr>
<tr>
<td>2</td>
<td>Sunday</td>
<td>1-53</td>
<td>with a Sunday in this year</td>
</tr>
<tr>
<td>3</td>
<td>Monday</td>
<td>1-53</td>
<td>with more than 3 days this year</td>
</tr>
<tr>
<td>4</td>
<td>Sunday</td>
<td>0-53</td>
<td>with more than 3 days this year</td>
</tr>
<tr>
<td>5</td>
<td>Monday</td>
<td>0-53</td>
<td>with a Monday in this year</td>
</tr>
<tr>
<td>6</td>
<td>Sunday</td>
<td>1-53</td>
<td>with more than 3 days this year</td>
</tr>
<tr>
<td>7</td>
<td>Monday</td>
<td>1-53</td>
<td>with a Monday in this year</td>
</tr>
</table>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT WEEK('1998-02-20');</code></strong>
        -&gt; 7
mysql&gt; <strong class="userinput"><code>SELECT WEEK('1998-02-20',0);</code></strong>
        -&gt; 7
mysql&gt; <strong class="userinput"><code>SELECT WEEK('1998-02-20',1);</code></strong>
        -&gt; 8
mysql&gt; <strong class="userinput"><code>SELECT WEEK('1998-12-31',1);</code></strong>
        -&gt; 53</pre>
<p>Note that if a date falls in the last week of the previous           year, MySQL returns <code class="literal">0</code> if you do not use           <code class="literal">2</code>, <code class="literal">3</code>,           <code class="literal">6</code>, or <code class="literal">7</code> as the optional           <em class="replaceable"><code>mode</code></em> argument:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);</code></strong>
        -&gt; 2000, 0</pre>
<p>One might argue that MySQL should return <code class="literal">52</code>           for the <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a> function,           because the given date actually occurs in the 52nd week of           1999. We decided to return <code class="literal">0</code> instead           because we want the function to return â€œ<span class="quote">the week number           in the given year.</span>â€ This makes use of the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a> function reliable when           combined with other functions that extract a date part from a           date.</p>
<p>If you would prefer the result to be evaluated with respect to           the year that contains the first day of the week for the given           date, use <code class="literal">0</code>, <code class="literal">2</code>,           <code class="literal">5</code>, or <code class="literal">7</code> as the optional           <em class="replaceable"><code>mode</code></em> argument.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT WEEK('2000-01-01',2);</code></strong>
        -&gt; 52</pre>
<p>Alternatively, use the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_yearweek"><code class="literal">YEARWEEK()</code></a> function:</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT YEARWEEK('2000-01-01');</code></strong>
        -&gt; 199952
mysql&gt; <strong class="userinput"><code>SELECT MID(YEARWEEK('2000-01-01'),5,2);</code></strong>
        -&gt; '52'</pre>
</li>
<li><a name="function_weekday"></a>           <a class="indexterm" name="id2847753"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_weekday"><code class="literal">WEEKDAY(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the weekday index for <em class="replaceable"><code>date</code></em>           (<code class="literal">0</code> = Monday, <code class="literal">1</code> =           Tuesday, â€¦ <code class="literal">6</code> = Sunday).</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT WEEKDAY('1998-02-03 22:23:00');</code></strong>
        -&gt; 1
mysql&gt; <strong class="userinput"><code>SELECT WEEKDAY('1997-11-05');</code></strong>
        -&gt; 2</pre>
</li>
<li><a name="function_weekofyear"></a>           <a class="indexterm" name="id2847856"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_weekofyear"><code class="literal">WEEKOFYEAR(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the calendar week of the date as a number in the range           from <code class="literal">1</code> to <code class="literal">53</code>.           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_weekofyear"><code class="literal">WEEKOFYEAR()</code></a> is a           compatibility function that is equivalent to           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK(<em class="replaceable"><code>date</code></em>,3)</code></a>.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT WEEKOFYEAR('1998-02-20');</code></strong>
        -&gt; 8</pre>
</li>
<li><a name="function_year"></a>           <a class="indexterm" name="id2847962"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_year"><code class="literal">YEAR(<em class="replaceable"><code>date</code></em>)</code></a>
<p>Returns the year for <em class="replaceable"><code>date</code></em>, in the           range <code class="literal">1000</code> to <code class="literal">9999</code>, or           <code class="literal">0</code> for the â€œ<span class="quote">zero</span>â€ date.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT YEAR('98-02-03');</code></strong>
        -&gt; 1998</pre>
</li>
<li><a name="function_yearweek"></a>           <a class="indexterm" name="id2848062"></a>            <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_yearweek"><code class="literal">YEARWEEK(<em class="replaceable"><code>date</code></em>)</code></a>,           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_yearweek"><code class="literal">YEARWEEK(<em class="replaceable"><code>date</code></em>,<em class="replaceable"><code>mode</code></em>)</code></a>
<p>Returns year and week for a date. The           <em class="replaceable"><code>mode</code></em> argument works exactly like           the <em class="replaceable"><code>mode</code></em> argument to           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a>. The year in the result           may be different from the year in the date argument for the           first and the last week of the year.</p>
<pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT YEARWEEK('1987-01-01');</code></strong>
        -&gt; 198653</pre>
<p>Note that the week number is different from what the           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a> function would return           (<code class="literal">0</code>) for optional arguments           <code class="literal">0</code> or <code class="literal">1</code>, as           <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week"><code class="literal">WEEK()</code></a> then returns the week           in the context of the given year.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://music.dreamvib.com/programming/mysql/date-and-time-functions-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysqldump</title>
		<link>http://music.dreamvib.com/programming/mysql/mysqldump/</link>
		<comments>http://music.dreamvib.com/programming/mysql/mysqldump/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 08:05:53 +0000</pubDate>
		<dc:creator>dreamvib</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://blog.dreamvib.com/?p=14</guid>
		<description><![CDATA[Name
mysqldump &#8211; a database backup program
Synopsis

mysqldump [options] [db_name [tbl_name ...]]

Description
The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or for transferring the data to another SQL server (not necessarily a MySQL server). The dump contains SQL statements [...]]]></description>
			<content:encoded><![CDATA[<h2>Name</h2>
<p>mysqldump &#8211; a database backup program</p>
<h2>Synopsis</h2>
<dl compact="compact">
<dt><strong>mysqldump [<em>options</em>] [<em>db_name</em> [<em>tbl_name</em> ...]]</strong></dt>
</dl>
<h2>Description</h2>
<p>The <strong>mysqldump</strong> client is a backup program originally written by Igor Romanenko. It can be used to <span class="highlight1">dump</span> a database or a collection of databases for backup or for transferring the data to another SQL server (not necessarily a <span class="highlight0">MySQL</span> server). The <span class="highlight1">dump</span> contains SQL statements to create the table or populate it, or both.</p>
<p>If you are doing a backup on the server, and your tables all are MyISAM tables, consider using the <strong>mysqlhotcopy</strong> instead because it can accomplish faster backups and faster restores. See <em><strong><a href="http://linux.die.net/man/1/mysqlhotcopy" rel="nofollow">mysqlhotcopy</a></strong>(1)</em>.</p>
<p>There are three general ways to invoke <strong>mysqldump</strong>:</p>
<pre class="code">shell&gt; mysqldump [options] db_name [tables]
shell&gt; mysqldump [options] --databases db_name1 [db_name2 db_name3...]
shell&gt; mysqldump [options] --all-databases</pre>
<p>If you do not name any tables following <em>db_name</em> or if you use the <strong>&#8211;databases</strong> or <strong>&#8211;all-databases</strong> option, entire databases are dumped.To get a list of the options your version of <strong>mysqldump</strong> supports, execute <strong>mysqldump &#8211;help</strong>.</p>
<p>If you run <strong>mysqldump</strong> without the <strong>&#8211;quick</strong> or <strong>&#8211;opt</strong> option, <strong>mysqldump</strong> loads the whole result set into memory before dumping the result. This can be a problem if you are dumping a big database. The <strong>&#8211;opt</strong> option is enabled by default, but can be disabled with <strong>&#8211;skip-opt</strong>.</p>
<p>If you are using a recent copy of the <strong>mysqldump</strong> program to generate a <span class="highlight1">dump</span> to be reloaded into a very old <span class="highlight0">MySQL</span> server, you should not use the <strong>&#8211;opt</strong> or <strong>&#8211;extended-insert</strong> option. Use <strong>&#8211;skip-opt</strong> instead.</p>
<p><strong>mysqldump</strong> supports the following options:</p>
<ul>
<li><strong>&#8211;help</strong>, <strong>-?</strong>Display a help message and exit.</li>
<li><strong>&#8211;add-drop-database</strong>Add a DROP DATABASE statement before each CREATE DATABASE statement.</li>
<li><strong>&#8211;add-drop-table</strong>Add a DROP TABLE statement before each CREATE TABLE statement.</li>
<li><strong>&#8211;add-locks</strong>Surround each table <span class="highlight1">dump</span> with LOCK TABLES and UNLOCK TABLES statements. This results <span class="highlight2">in</span> faster inserts when the <span class="highlight1">dump</span> file is reloaded. See Section 2.16, lqSpeed of INSERT Statementsrq.</li>
<li><strong>&#8211;all-databases</strong>, <strong>-A</strong><span class="highlight1">Dump</span> all tables <span class="highlight2">in</span> all databases. This is the same as using the <strong>&#8211;databases</strong> option and naming all the databases on the command line.</li>
<li><strong>&#8211;allow-keywords</strong>Allow creation of column names that are keywords. This works by prefixing each column name with the table name.</li>
<li><strong>&#8211;character-sets-dir=<em>path</em></strong>The directory where character sets are installed. See Section 9.1, lqThe Character Set Used for Data and Sortingrq.</li>
<li><strong>&#8211;comments</strong>, <strong>-i</strong>Write additional information <span class="highlight2">in</span> the <span class="highlight1">dump</span> file such as program version, server version, and host. . This option is enabled by default. To suppress additional, use <strong>&#8211;skip-comments</strong>.</li>
<li><strong>&#8211;compact</strong>Produce less verbose output. This option suppresses comments and enables the <strong>&#8211;skip-add-drop-table</strong>, <strong>&#8211;no-set-names</strong>, <strong>&#8211;skip-disable-keys</strong>, and <strong>&#8211;skip-add-locks</strong> options.</li>
<li><strong>&#8211;compatible=<em>name</em></strong>Produce output that is more compatible with other database systems or with older <span class="highlight0">MySQL</span> servers. The value of name can be ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, or no_field_options. To use several values, separate them by commas. These values have the same meaning as the corresponding options for setting the server SQL mode. See the section called lqTHE SERVER SQL MODErq.
<p>This option does not guarantee compatibility with other servers. It only enables those SQL mode values that are currently available for making <span class="highlight1">dump</span> output more compatible. For example, <strong>&#8211;compatible=oracle</strong> does not map data types to Oracle types or use Oracle comment syntax.</li>
<li><strong>&#8211;complete-insert</strong>, <strong>-c</strong>Use complete INSERT statements that include column names.</li>
<li><strong>&#8211;compress</strong>, <strong>-C</strong>Compress all information sent between the client and the server if both support compression.</li>
<li><strong>&#8211;create-options</strong>Include all <span class="highlight0">MySQL</span>-specific table options <span class="highlight2">in</span> the CREATE TABLE statements.</li>
<li><strong>&#8211;databases</strong>, <strong>-B</strong><span class="highlight1">Dump</span> several databases. Normally, <strong>mysqldump</strong> treats the first name argument on the command line as a database name and following names as table names. With this option, it treats all name arguments as database names. CREATE DATABASE and USE statements are included <span class="highlight2">in</span> the output before each new database.</li>
<li><strong>&#8211;debug[=<em>debug_options</em>]</strong>, <strong>-# [<em>debug_options</em>]</strong>Write a debugging log. The <em>debug_options</em> string is often &#8216;d:t:o,<em>file_name</em>&#8216;. The default is &#8216;d:t:o,/tmp/mysqldump.trace&#8217;.</li>
<li><strong>&#8211;default-character-set=<em>charset_name</em></strong>Use <em>charset_name</em> as the default character set. See Section 9.1, lqThe Character Set Used for Data and Sortingrq. If not specified, <strong>mysqldump</strong> uses utf8.</li>
<li><strong>&#8211;delayed-insert</strong>Write INSERT DELAYED statements rather than INSERT statements.</li>
<li><strong>&#8211;delete-master-logs</strong>On a master replication server, delete the binary logs after performing the <span class="highlight1">dump</span> operation. This option automatically enables <strong>&#8211;master-data</strong>.</li>
<li><strong>&#8211;disable-keys</strong>, <strong>-K</strong>For each table, surround the INSERT statements with /*!40000 ALTER TABLE <em>tbl_name</em> DISABLE KEYS */; and /*!40000 ALTER TABLE <em>tbl_name</em> ENABLE KEYS */; statements. This makes loading the <span class="highlight1">dump</span> file faster because the indexes are created after all rows are inserted. This option is effective for MyISAM tables only.</li>
<li><strong>&#8211;extended-insert</strong>, <strong>-e</strong>Use multiple-row INSERT syntax that include several VALUES lists. This results <span class="highlight2">in</span> a smaller <span class="highlight1">dump</span> file and speeds up inserts when the file is reloaded.</li>
<li><strong>&#8211;fields-terminated-by=&#8230;</strong>, <strong>&#8211;fields-enclosed-by=&#8230;</strong>, <strong>&#8211;fields-optionally-enclosed-by=&#8230;</strong>, <strong>&#8211;fields-escaped-by=&#8230;</strong>, <strong>&#8211;lines-terminated-by=&#8230;</strong>These options are used with the <strong>-T</strong> option and have the same meaning as the corresponding clauses for LOAD DATA INFILE. See Section 2.5, lqLOAD DATA INFILE Syntaxrq.</li>
<li><strong>&#8211;first-slave</strong>, <strong>-x</strong>Deprecated. Now renamed to <strong>&#8211;lock-all-tables</strong>.</li>
<li><strong>&#8211;flush-logs</strong>, <strong>-F</strong>Flush the <span class="highlight0">MySQL</span> server log files before starting the <span class="highlight1">dump</span>. This option requires the RELOAD privilege. Note that if you use this option <span class="highlight2">in</span> combination with the <strong>&#8211;all-databases</strong> (or <strong>-A</strong>) option, the logs are flushed <em>for each database dumped</em>. The exception is when using <strong>&#8211;lock-all-tables</strong> or <strong>&#8211;master-data</strong>: <span class="highlight2">In</span> this case, the logs are flushed only once, corresponding to the moment that all tables are locked. If you want your <span class="highlight1">dump</span> and the log flush to happen at exactly the same moment, you should use <strong>&#8211;flush-logs</strong> together with either <strong>&#8211;lock-all-tables</strong> or <strong>&#8211;master-data</strong>.</li>
<li><strong>&#8211;force</strong>, <strong>-f</strong>Continue even if an SQL error occurs during a table <span class="highlight1">dump</span>.</li>
<li><strong>&#8211;host=<em>host_name</em></strong>, <strong>-h <em>host_name</em></strong><span class="highlight1">Dump</span> data from the <span class="highlight0">MySQL</span> server on the given host. The default host is localhost.</li>
<li><strong>&#8211;hex-blob</strong><span class="highlight1">Dump</span> binary columns using hexadecimal notation (for example, &#8216;abc&#8217; becomes 0&#215;616263). The affected data types are BINARY, VARBINARY, and BLOB. As of <span class="highlight0">MySQL</span> 5.0.13, BIT columns are affected as well.</li>
<li><strong>&#8211;ignore-table=<em>db_name.tbl_name</em></strong>Do not <span class="highlight1">dump</span> the given table, which must be specified using both the database and table names. To ignore multiple tables, use this option multiple times.</li>
<li><strong>&#8211;insert-ignore</strong>Write INSERT statements with the IGNORE option.</li>
<li><strong>&#8211;lock-all-tables</strong>, <strong>-x</strong>Lock all tables across all databases. This is achieved by acquiring a global read lock for the duration of the whole <span class="highlight1">dump</span>. This option automatically turns off <strong>&#8211;single-transaction</strong> and <strong>&#8211;lock-tables</strong>.</li>
<li><strong>&#8211;lock-tables</strong>, <strong>-l</strong>Lock all tables before starting the <span class="highlight1">dump</span>. The tables are locked with READ LOCAL to allow concurrent inserts <span class="highlight2">in</span> the case of MyISAM tables. For transactional tables such as InnoDB and BDB, <strong>&#8211;single-transaction</strong> is a much better option, because it does not need to lock the tables at all.
<p>Please note that when dumping multiple databases, <strong>&#8211;lock-tables</strong> locks tables for each database separately. So, this option does not guarantee that the tables <span class="highlight2">in</span> the <span class="highlight1">dump</span> file are logically consistent between databases. Tables <span class="highlight2">in</span> different databases may be dumped <span class="highlight2">in</span> completely different states.</li>
<li><strong>&#8211;master-data[=<em>value</em>]</strong>Write the binary log filename and position to the output. This option requires the RELOAD privilege and the binary log must be enabled. If the option value is equal to 1, the position and filename are written to the <span class="highlight1">dump</span> output <span class="highlight2">in</span> the form of a CHANGE MASTER statement that makes a slave server start from the correct position <span class="highlight2">in</span> the master&#8217;s binary logs if you use this SQL <span class="highlight1">dump</span> of the master to set up a slave. If the option value is equal to 2, the CHANGE MASTER statement is written as an SQL comment. This is the default action if <em>value</em> is omitted.
<p>The <strong>&#8211;master-data</strong> option turns on <strong>&#8211;lock-all-tables</strong>, unless <strong>&#8211;single-transaction</strong> also is specified (<span class="highlight2">in</span> which case, a global read lock is only acquired a short time at the beginning of the <span class="highlight1">dump</span>. See also the description for <strong>&#8211;single-transaction</strong>. <span class="highlight2">In</span> all cases, any action on logs happens at the exact moment of the <span class="highlight1">dump</span>. This option automatically turns off <strong>&#8211;lock-tables</strong>.</li>
<li><strong>&#8211;no-autocommit</strong>Enclose the INSERT statements for each dumped table within SET AUTOCOMMIT=0 and COMMIT statements.</li>
<li><strong>&#8211;no-create-db</strong>, <strong>-n</strong>This option suppresses the CREATE DATABASE statements that are otherwise included <span class="highlight2">in</span> the output if the <strong>&#8211;databases</strong> or <strong>&#8211;all-databases</strong> option is given.</li>
<li><strong>&#8211;no-create-info</strong>, <strong>-t</strong>Do not write CREATE TABLE statements that re-create each dumped table.</li>
<li><strong>&#8211;no-data</strong>, <strong>-d</strong>Do not write any row information for the table. This is very useful if you want to <span class="highlight1">dump</span> only the CREATE TABLE statement for the table.</li>
<li><strong>&#8211;opt</strong>This option is shorthand; it is the same as specifying <strong>&#8211;add-drop-table &#8211;add-locks &#8211;create-options &#8211;disable-keys &#8211;extended-insert &#8211;lock-tables &#8211;quick &#8211;set-charset</strong>. It should give you a fast <span class="highlight1">dump</span> operation and produce a <span class="highlight1">dump</span> file that can be reloaded into a <span class="highlight0">MySQL</span> server quickly.
<p><em>The <strong>&#8211;opt</strong> option is enabled by default. To disable the options that it enables, use <strong>&#8211;skip-opt</strong></em>. To disable only certain of the options enabled by <strong>&#8211;opt</strong>, use their <strong>&#8211;skip</strong> forms; for example, <strong>&#8211;skip-add-drop-table</strong> or <strong>&#8211;skip-quick</strong>.</li>
<li><strong>&#8211;order-by-primary</strong>Sorts each table&#8217;s rows by its primary key, or its first unique index, if such an index exists. This is useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the <span class="highlight1">dump</span> itself take considerably longer.</li>
<li><strong>&#8211;password[=<em>password</em>]</strong>, <strong>-p[<em>password</em>]</strong>The password to use when connecting to the server. If you use the short option form (<strong>-p</strong>), you <em>cannot</em> have a space between the option and the password. If you omit the <em>password</em> value following the <strong>&#8211;password</strong> or <strong>-p</strong> option on the command line, you are prompted for one.
<p>Specifying a password on the command line should be considered insecure. See Section 7.6, lqKeeping Your Password Securerq.</li>
<li style="list-style-type: none; list-style-image: none; list-style-position: outside">Â·</li>
<li><strong>&#8211;port=<em>port_num</em></strong>, <strong>-P <em>port_num</em></strong>The TCP/IP port number to use for the connection.</li>
<li><strong>&#8211;protocol={TCP|SOCKET|PIPE|MEMORY}</strong>The connection protocol to use.</li>
<li><strong>&#8211;quick</strong>, <strong>-q</strong>This option is useful for dumping large tables. It forces <strong>mysqldump</strong> to retrieve rows for a table from the server a row at a time rather than retrieving the entire row set and buffering it <span class="highlight2">in</span> memory before writing it out.</li>
<li><strong>&#8211;quote-names</strong>, <strong>-Q</strong>Quote database, table, and column names within oq&#8217;cq characters. If the ANSI_QUOTES SQL mode is enabled, names are quoted within oq&#8221;cq characters. This option is enabled by default. It can be disabled with <strong>&#8211;skip-quote-names</strong>, but this option should be given after any option such as <strong>&#8211;compatible</strong> that may enable <strong>&#8211;quote-names</strong>.</li>
<li><strong>&#8211;result-file=<em>file</em></strong>, <strong>-r <em>file</em></strong>Direct output to a given file. This option should be used on Windows to prevent newline oq\ncq characters from being converted to oq\r\ncq carriage return/newline sequences.</li>
<li><strong>&#8211;routines</strong>, <strong>-R</strong><span class="highlight1">Dump</span> stored routines (functions and procedures) from the dumped databases. The output generated by using <strong>&#8211;routines</strong> contains CREATE PROCEDURE and CREATE FUNCTION statements to re-create the routines. However, these statements do not include attributes such as the routine creation and modification timestamps. This means that when the routines are reloaded, they will be created with the timestamps equal to the reload time.
<p>If you require routines to be re-created with their original timestamp attributes, do not use <strong>&#8211;routines</strong>. Instead, <span class="highlight1">dump</span> and reload the contents of the <span class="highlight0">mysql</span>.proc table directly, using a <span class="highlight0">MySQL</span> account that has appropriate privileges for the <span class="highlight0">mysql</span> database.</p>
<p>This option was added <span class="highlight2">in</span> <span class="highlight0">MySQL</span> 5.0.13. Before that, stored routines are not dumped. Routine DEFINER values are not dumped until <span class="highlight0">MySQL</span> 5.0.20. This means that before 5.0.20, when routines are reloaded, they will be created with the definer set to the reloading user. If you require routines to be re-created with their original definer, <span class="highlight1">dump</span> and load the contents of the <span class="highlight0">mysql</span>.proc table directly as described earlier.</li>
<li><strong>&#8211;set-charset</strong>Add SET NAMES <em>default_character_set</em> to the output. This option is enabled by default. To suppress the SET NAMES statement, use <strong>&#8211;skip-set-charset</strong>.</li>
<li><strong>&#8211;single-transaction</strong>This option issues a BEGIN SQL statement before dumping data from the server. It is useful only with transactional tables such as InnoDB and BDB, because then it <span class="highlight1">dumps</span> the consistent state of the database at the time when BEGIN was issued without blocking any applications.
<p>When using this option, you should keep <span class="highlight2">in</span> mind that only InnoDB tables are dumped <span class="highlight2">in</span> a consistent state. For example, any MyISAM or MEMORY tables dumped while using this option may still change state.</p>
<p>The <strong>&#8211;single-transaction</strong> option and the <strong>&#8211;lock-tables</strong> option are mutually exclusive, because LOCK TABLES causes any pending transactions to be committed implicitly.</p>
<p>To <span class="highlight1">dump</span> big tables, you should combine this option with <strong>&#8211;quick</strong>.</li>
<li><strong>&#8211;socket=<em>path</em></strong>, <strong>-S <em>path</em></strong>For connections to localhost, the Unix socket file to use, or, on Windows, the name of the named pipe to use.</li>
<li><strong>&#8211;skip-comments</strong>See the description for the <strong>&#8211;comments</strong> option.</li>
<li><strong>&#8211;ssl*</strong>Options that begin with <strong>&#8211;ssl</strong> specify whether to connect to the server via SSL and indicate where to find SSL keys and certificates. See Section 7.7.3, lqSSL Command Optionsrq.</li>
<li style="list-style-type: none; list-style-image: none; list-style-position: outside">Â·</li>
<li><strong>&#8211;tab=<em>path</em></strong>, <strong>-T <em>path</em></strong>Produce tab-separated data files. For each dumped table, <strong>mysqldump</strong> creates a <em>tbl_name.sql</em> file that contains the CREATE TABLE statement that creates the table, and a <em>tbl_name.txt</em> file that contains its data. The option value is the directory <span class="highlight2">in</span> which to write the files.
<p>By default, the <em>.txt</em> data files are formatted using tab characters between column values and a newline at the end of each line. The format can be specified explicitly using the <strong>&#8211;fields-<em>xxx</em></strong> and <strong>&#8211;lines&#8211;<em>xxx</em></strong> options.</p>
<p><strong>Note</strong>: This option should be used only when <strong>mysqldump</strong> is run on the same machine as the <strong>mysqld</strong> server. You must have the FILE privilege, and the server must have permission to write files <span class="highlight2">in</span> the directory that you specify.</li>
<li><strong>&#8211;tables</strong>Override the <strong>&#8211;databases</strong> or <strong>-B</strong> option. All name arguments following the option are regarded as table names.</li>
<li><strong>&#8211;triggers</strong><span class="highlight1">Dump</span> triggers for each dumped table. This option is enabled by default; disable it with <strong>&#8211;skip-triggers</strong>. This option was added <span class="highlight2">in</span> <span class="highlight0">MySQL</span> 5.0.11. Before that, triggers are not dumped.</li>
<li><strong>&#8211;tz-utc</strong>Add SET TIME_ZONE=&#8217;+00:00&#8242; to the <span class="highlight1">dump</span> file so that TIMESTAMP columns can be dumped and reloaded between servers <span class="highlight2">in</span> different time zones. Without this option, TIMESTAMP columns are dumped and reloaded <span class="highlight2">in</span> the time zones local to the source and destination servers, which can cause the values to change. <strong>&#8211;tz-utc</strong> also protects against changes due to daylight saving time. <strong>&#8211;tz-utc</strong> is enabled by default. To disable it, use <strong>&#8211;skip-tz-utc</strong>. This option was added <span class="highlight2">in</span> <span class="highlight0">MySQL</span> 5.0.15.</li>
<li><strong>&#8211;user=<em>user_name</em></strong>, <strong>-u <em>user_name</em></strong>The <span class="highlight0">MySQL</span> username to use when connecting to the server.</li>
<li><strong>&#8211;verbose</strong>, <strong>-v</strong>Verbose mode. Print more information about what the program does.</li>
<li><strong>&#8211;version</strong>, <strong>-V</strong>Display version information and exit.</li>
<li><strong>&#8211;where=&#8217;<em>where_condition</em>&#8216;</strong>, <strong>-w &#8216;<em>where_condition</em>&#8216;</strong><span class="highlight1">Dump</span> only rows selected by the given WHERE condition. Note that quotes around the condition are mandatory if it contains spaces or other characters that are special to your command interpreter.
<p>Examples:</p>
<pre class="code">--where="user='jimf'"
-w"userid&gt;1"
-w"userid&lt;1"</pre>
</li>
<li><strong>&#8211;xml</strong>, <strong>-X</strong>Write <span class="highlight1">dump</span> output as well-formed XML.</li>
</ul>
<p>You can also set the following variables by using <strong>&#8211;<em>var_name</em>=<em>value</em></strong> syntax:</p>
<ul>
<li>max_allowed_packetThe maximum size of the buffer for client/server communication. The maximum is 1GB.</li>
<li>net_buffer_lengthThe initial size of the buffer for client/server communication. When creating multiple-row-insert statements (as with option <strong>&#8211;extended-insert</strong> or <strong>&#8211;opt</strong>), <strong>mysqldump</strong> creates rows up to net_buffer_length length. If you increase this variable, you should also ensure that the net_buffer_length variable <span class="highlight2">in</span> the <span class="highlight0">MySQL</span> server is at least this large.</li>
</ul>
<p>It is also possible to set variables by using <strong>&#8211;set-variable=<em>var_name</em>=<em>value</em></strong> or <strong>-O <em>var_name</em>=<em>value</em></strong> syntax. <em>This syntax is deprecated</em>.</p>
<p>The most common use of <strong>mysqldump</strong> is probably for making a backup of an entire database:</p>
<pre class="code">shell&gt; mysqldump --opt db_name &gt; backup-file.sql</pre>
<p>You can read the <span class="highlight1">dump</span> file back into the server like this:</p>
<pre class="code">shell&gt; <span class="highlight0">mysql</span> db_name &lt; backup-file.sql</pre>
<p>Or like this:</p>
<pre class="code">shell&gt; <span class="highlight0">mysql</span> -e "source /path-to-backup/backup-file.sql" db_name</pre>
<p><strong>mysqldump</strong> is also very useful for populating databases by copying data from one <span class="highlight0">MySQL</span> server to another:</p>
<pre class="code">shell&gt; mysqldump --opt db_name | <span class="highlight0">mysql</span> --host=remote_host -C db_name</pre>
<p>It is possible to <span class="highlight1">dump</span> several databases with one command:</p>
<pre class="code">shell&gt; mysqldump --databases db_name1 [db_name2 ...] &gt; my_databases.sql</pre>
<p>To <span class="highlight1">dump</span> all databases, use the <strong>&#8211;all-databases</strong> option:</p>
<pre class="code">shell&gt; mysqldump --all-databases &gt; all_databases.sql</pre>
<p>For InnoDB tables, mysqldump provides a way of making an online backup:</p>
<pre class="code">shell&gt; mysqldump --all-databases --single-transaction &gt; all_databases.sql</pre>
<p>This backup just needs to acquire a global read lock on all tables (using FLUSH TABLES WITH READ LOCK) at the beginning of the <span class="highlight1">dump</span>. As soon as this lock has been acquired, the binary log coordinates are read and the lock is released. If and only if one long updating statement is running when the FLUSH statement is issued, the <span class="highlight0">MySQL</span> server may get stalled until that long statement finishes, and then the <span class="highlight1">dump</span> becomes lock-free. If the update statements that the <span class="highlight0">MySQL</span> server receives are short (<span class="highlight2">in</span> terms of execution time), the initial lock period should not be noticeable, even with many updates.For point-<span class="highlight2">in</span>-time recovery (also known as lqroll-forward,rq when you need to restore an old backup and replay the changes that happened since that backup), it is often useful to rotate the binary log (see Section 10.3, lqThe Binary Logrq) or at least know the binary log coordinates to which the <span class="highlight1">dump</span> corresponds:</p>
<pre class="code">shell&gt; mysqldump --all-databases --master-data=2 &gt; all_databases.sql</pre>
<p>Or:</p>
<pre class="code">shell&gt; mysqldump --all-databases --flush-logs --master-data=2
              &gt; all_databases.sql</pre>
<p>The simultaneous use of <strong>&#8211;master-data</strong> and <strong>&#8211;single-transaction</strong> provides a convenient way to make an online backup suitable for point-<span class="highlight2">in</span>-time recovery if tables are stored <span class="highlight2">in</span> the InnoDB storage engine.</p>
<p>source: <a href="http://linux.die.net/man/1/mysqldump" target="_blank">http://linux.die.net/man/1/mysqldump</a></p>
]]></content:encoded>
			<wfw:commentRss>http://music.dreamvib.com/programming/mysql/mysqldump/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
