cnblogs/dcrenl/SQL 时间函数 Datepart()与DateName().html
2024-09-24 12:43:01 +08:00

182 lines
15 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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 id="sina_keyword_ad_area2" class="articalContent ">
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
1.Datepart()</p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
<strong>返回代表指定日期的指定日期部分的整数</strong></p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
<strong>语法 Datepart(datepart,date)</strong></p>
<h5 style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; text-indent:23px">
返回类型&nbsp;<wbr><strong>int</strong></h5>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
datepart:</p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
&nbsp;<wbr></p>
<table cellpadding="2" rules="all" border="1" frame="box" style="table-layout:auto!important; width:571px">
<tbody>
<tr valign="top">
<th width="44%">日期部分</th>
<th width="56%">缩写</th>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>year</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>yy, yyyy</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>quarter</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>qq, q</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>month</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>mm, m</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>dayofyear</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>dy, y</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>day</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>dd, d</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>week</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>wk, ww</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>weekday</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>dw</strong><span style="font-size:10px">日期部分返回对应于星期中的某天的数例如Sunday =1</span></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>Hour</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>hh</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>minute</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>mi, n</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>second</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>ss, s</strong></td>
</tr>
<tr valign="top">
<td width="44%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>millisecond</strong></td>
<td width="56%" style="font-family:Arial; word-wrap:break-word; word-break:break-all; visibility:visible!important; zoom:1!important; font-size:12px; line-height:normal">
<strong>ms</strong></td>
</tr>
</tbody>
</table>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
declare @date datetime<br style="line-height:normal">
set @date='2009-12-24 16:35:37.733'<br style="line-height:normal">
select datepart(year,@date)--2009<br style="line-height:normal">
select datepart(month,@date)--12<br style="line-height:normal">
select datepart(day,@date)--24<br style="line-height:normal">
select datepart(quarter,@date)--4<br style="line-height:normal">
select datepart(dayofyear,@date)--358<br style="line-height:normal">
select datepart(weekday,@date)--5<br style="line-height:normal">
select datepart(week,@date)--52<br style="line-height:normal">
select datepart(Hour,@date)--16<br style="line-height:normal">
select datepart(minute,@date)--35</p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
2.DateName()</p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
<strong>返回代表指定日期的指定日期部分的字符串</strong></p>
<h5 style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; text-indent:23px">
语法 DateName<code><span style="font-family:新宋体"><strong>(datepart</strong><em>&nbsp;<wbr></em><strong>,</strong>&nbsp;<wbr><em>date&nbsp;<wbr></em><strong>)</strong></span></code></h5>
<h5><code style="line-height:normal; color:rgb(102,102,102); font-size:12px; text-indent:23px"><span style="font-family:新宋体"><strong>返回类型&nbsp;<wbr></strong>nvarchar</span></code></h5>
<h5 style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; text-indent:23px">
参数&nbsp;<wbr><em>datepart 同上表&#26684;</em></h5>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
declare @date datetime<br style="line-height:normal">
set @date='2009-12-24 16:35:37.733'<br style="line-height:normal">
select datename(year,@date)--2009<br style="line-height:normal">
select datename(month,@date)--December<br style="line-height:normal">
select datename(day,@date)--24<br style="line-height:normal">
select datename(quarter,@date)--4<br style="line-height:normal">
select datename(dayofyear,@date)--358<br style="line-height:normal">
select datename(weekday,@date)--Thursday<br style="line-height:normal">
select datename(week,@date)--52<br style="line-height:normal">
select datename(Hour,@date)--16<br style="line-height:normal">
select datename(minute,@date)--35</p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
<em>3.其他日期函数</em></p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
declare @date1 datetime,@date2 datetime<br style="line-height:normal">
set @date1='2009-10-24 16:35:37.733'<br style="line-height:normal">
set @date2='2009-12-24 16:35:37.733'<br style="line-height:normal">
select DAY(@date1)--24<br style="line-height:normal">
select MONTH(@date1)--10 具有确定性&nbsp;<wbr><br style="line-height:normal">
select YEAR (@date1)--2009具有确定性&nbsp;<wbr><br style="line-height:normal">
select DATEPART (dw, @date2)</p>
<p style="line-height:normal; color:rgb(102,102,102); font-family:Georgia; font-size:12px; text-indent:23px">
select GETDATE() 不具有确定性&nbsp;<wbr><br style="line-height:normal">
select GETUTCDATE() 不具有确定性<br style="line-height:normal">
DATENAME() 不具有确定性<br style="line-height:normal">
DATEADD(dd,@date1,1) 具有确定性&nbsp;<wbr><br style="line-height:normal">
DATEDIFF 具有确定性<br style="line-height:normal">
DATEPART 除了用作&nbsp;<wbr><br style="line-height:normal">
DATEPART (dw, date) 外都具有确定性。dw 是工作日的日期部分,取决于由设置每周第一天的 SET DATEFIRST所设置的&#20540;&nbsp;<wbr><br style="line-height:normal">
具有确定性</p>
<p style="text-indent:23px">&nbsp;<wbr></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--判断是否为时间</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectcast(isdate('2012-06-02 16:26:07.880') asvarchar)</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--获得年</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">select cast(year('2012-06-0216:26:07.880') as varchar)</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(yyyy,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(yyyy,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--获得月</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">select cast(Month('2012-06-0216:26:07.880') as varchar)</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(mm,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(mm,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--获得日</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">select cast(day('2012-06-0216:26:07.880') as varchar)</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(dd,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(dd,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--获得时</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(hh,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(hh,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--获得分</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">select datepart(n,'2012-06-0216:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">select datename(n,'2012-06-0216:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--获得秒</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(ss,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(ss,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--毫秒</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(ms,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(ms,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--季节</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(qq,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(qq,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--一年中的第几天</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(dy,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(dy,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--一年中的第几周</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(wk,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(wk,'2012-06-02 16:26:07.880')</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">--星期</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatepart(dw,'2012-06-02 16:26:07.880') --SET DATEFIRST 7每周第一天</span></span></p>
<p><span style="color:#666666"><span style="font-size:12px; line-height:18px">selectdatename(dw,'2012-06-02 16:26:07.880')</span></span></p>
</div>