uTools-Manuals/docs/sql/Date 函数.html
2019-05-07 10:15:08 +08:00

181 lines
5.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="m-bg">
<h1>SQL Date 函数</h1>
<h2>SQL 日期Dates</h2>
<p><span aria-hidden="true" class="g-bg glyphicon glyphicon-bell margin-l-5"></span>当我们处理日期时,最难的任务恐怕是确保所插入的日期的格式,与数据库中日期列的格式相匹配。</p>
<p>只要您的数据包含的只是日期部分,运行查询就不会出问题。但是,如果涉及时间部分,情况就有点复杂了。</p>
<p>在讨论日期查询的复杂性之前,我们先来看看最重要的内建日期处理函数。</p>
<h2>MySQL Date 函数</h2>
<p>下面的表格列出了 MySQL 中最重要的内建日期函数:</p>
<table class="reference notranslate"><tbody>
<tr class="firstRow">
<th align="left" width="22%">函数</th>
<th align="left" width="78%">描述</th>
</tr>
<tr>
<td>NOW()</td>
<td>返回当前的日期和时间</td>
</tr>
<tr>
<td>CURDATE()</td>
<td>返回当前的日期</td>
</tr>
<tr>
<td>CURTIME()</td>
<td>返回当前的时间</td>
</tr>
<tr>
<td>DATE()</td>
<td>提取日期或日期/时间表达式的日期部分</td>
</tr>
<tr>
<td>EXTRACT()</td>
<td>返回日期/时间的单独部分</td>
</tr>
<tr>
<td>DATE_ADD()</td>
<td>向日期添加指定的时间间隔</td>
</tr>
<tr>
<td>DATE_SUB()</td>
<td>从日期减去指定的时间间隔</td>
</tr>
<tr>
<td>DATEDIFF()</td>
<td>返回两个日期之间的天数</td>
</tr>
<tr>
<td>DATE_FORMAT()</td>
<td>用不同的格式显示日期/时间</td>
</tr>
</tbody></table>
<h2>SQL Server Date 函数</h2>
<p>下面的表格列出了 SQL Server 中最重要的内建日期函数:</p>
<table class="reference notranslate"><tbody>
<tr class="firstRow">
<th align="left" width="22%">函数</th>
<th align="left" width="78%">描述</th>
</tr>
<tr>
<td>GETDATE()</td>
<td>返回当前的日期和时间</td>
</tr>
<tr>
<td>DATEPART()</td>
<td>返回日期/时间的单独部分</td>
</tr>
<tr>
<td>DATEADD()</td>
<td>在日期中添加或减去指定的时间间隔</td>
</tr>
<tr>
<td>DATEDIFF()</td>
<td>返回两个日期之间的时间</td>
</tr>
<tr>
<td>CONVERT()</td>
<td>用不同的格式显示日期/时间</td>
</tr>
</tbody></table>
<h2>SQL Date 数据类型</h2>
<p><strong>MySQL</strong> 使用下列数据类型在数据库中存储日期或日期/时间值:</p>
<ul class="list-group">
<li class="list-group-item">DATE - 格式YYYY-MM-DD</li>
<li class="list-group-item">DATETIME - 格式YYYY-MM-DD HH:MM:SS</li>
<li class="list-group-item">TIMESTAMP - 格式YYYY-MM-DD HH:MM:SS</li>
<li class="list-group-item">YEAR - 格式YYYY 或 YY</li>
</ul>
<p><strong>SQL Server</strong> 使用下列数据类型在数据库中存储日期或日期/时间值:</p>
<ul class="list-group">
<li class="list-group-item">DATE - 格式YYYY-MM-DD</li>
<li class="list-group-item">DATETIME - 格式YYYY-MM-DD HH:MM:SS</li>
<li class="list-group-item">SMALLDATETIME - 格式YYYY-MM-DD HH:MM:SS</li>
<li class="list-group-item">TIMESTAMP - 格式:唯一的数字</li>
</ul>
<p><strong>注释:</strong>当您在数据库中创建一个新表时,需要为列选择数据类型!</p>
<p>如需了解所有可用的数据类型,请访问我们完整的 数据类型参考手册。</p>
<h2>SQL 日期处理</h2>
<p><span aria-hidden="true" class="g-bg glyphicon glyphicon-bell margin-l-5"></span>如果不涉及时间部分,那么我们可以轻松地比较两个日期!</p>
<p>假设我们有如下的 "Orders" 表:</p>
<table class="reference notranslate"><tbody>
<tr class="firstRow">
<th align="left" width="20%">OrderId</th>
<th align="left" width="45%">ProductName</th>
<th align="left" width="35%">OrderDate</th>
</tr>
<tr>
<td>1</td>
<td>Geitost</td>
<td>2008-11-11</td>
</tr>
<tr>
<td>2</td>
<td>Camembert Pierrot</td>
<td>2008-11-09</td>
</tr>
<tr>
<td>3</td>
<td>Mozzarella di Giovanni</td>
<td>2008-11-11</td>
</tr>
<tr>
<td>4</td>
<td>Mascarpone Fabioli</td>
<td>2008-10-29</td>
</tr>
</tbody></table>
<p>现在,我们希望从上表中选取 OrderDate 为 "2008-11-11" 的记录。</p>
<p>我们使用下面的 SELECT 语句:</p>
<div class="code notranslate"><pre><code class="language-sql">SELECT * FROM Orders WHERE OrderDate='2008-11-11'</code></pre></div>
<p>结果集如下所示:</p>
<table class="reference notranslate"><tbody>
<tr class="firstRow">
<th align="left" width="20%">OrderId</th>
<th align="left" width="45%">ProductName</th>
<th align="left" width="35%">OrderDate</th>
</tr>
<tr>
<td>1</td>
<td>Geitost</td>
<td>2008-11-11</td>
</tr>
<tr>
<td>3</td>
<td>Mozzarella di Giovanni</td>
<td>2008-11-11</td>
</tr>
</tbody></table>
<p>现在,假设 "Orders" 表如下所示(请注意 "OrderDate" 列中的时间部分):</p>
<table class="reference notranslate"><tbody>
<tr class="firstRow">
<th align="left" width="20%">OrderId</th>
<th align="left" width="45%">ProductName</th>
<th align="left" width="35%">OrderDate</th>
</tr>
<tr>
<td>1</td>
<td>Geitost</td>
<td>2008-11-11 13:23:44</td>
</tr>
<tr>
<td>2</td>
<td>Camembert Pierrot</td>
<td>2008-11-09 15:45:21</td>
</tr>
<tr>
<td>3</td>
<td>Mozzarella di Giovanni</td>
<td>2008-11-11 11:12:01</td>
</tr>
<tr>
<td>4</td>
<td>Mascarpone Fabioli</td>
<td>2008-10-29 14:56:59</td>
</tr>
</tbody></table>
<p>如果我们使用和上面一样的 SELECT 语句:</p>
<div class="code notranslate"><pre><code class="language-sql">SELECT * FROM Orders WHERE OrderDate='2008-11-11'</code></pre></div>
<p>那么我们将得不到结果!这是由于该查询的日期不含有时间部分。</p>
<p><strong>提示:</strong>如果您希望使查询简单且更易维护,那么请不要在日期中使用时间部分!</p>
<p><br/></p> <div class="text-center padding-10 margin-t-5">
</div>
</div>