MS SQL datepart,datename

DATEPART:   返回代表指定日期的指定日期部分的整数。 

语法
DATEPART ( datepart , date )
 

DATENAME: 返回代表指定日期的指定日期部分的字符串。

DATENAME ( datepart , date )

datepart

是指定应返回的日期部分的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。

在DATAPART中

week (wk, ww) 日期部分反映对 SET DATEFIRST 作的更改。任何一年的 1 月 1 日定义了 week 日期部分的开始数字,例如:DATEPART(wk, 'Jan 1, xxxx') = 1,此处 xxxx 代表任一年。
weekday (dw) 日期部分返回对应于星期中的某天的数,例如:Sunday = 1、Saturday = 7。weekday 日期部分产生的数取决于 SET DATEFIRST 设定的值,此命令设定星期中的第一天。

date
是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。对 1753 年 1 月 1 日之后的日期用datetime 数据类型。更早的日期存储为字符数据。当输入 datetime 值时,始终将其放入引号中。因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。
如果只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049 (默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。
有关时间值指定的更多信息,请参见时间格式。有关日期指定的更多信息,请参见 datetime 和 smalldatetime。 
返回类型
int
注释

在DATENAME中:

weekday (dw) 日期部分返回星期几(星期天、星期一等)。

是返回 datetimesmalldatetime 值或日期格式字符串的表达式。对 1753 年 1 月 1 日之后的日期用datetime 数据类型。更早的日期存储为字符数据。当输入 datetime 值时,始终将其放入引号中。因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。有关指定日期的更多信息,请参见 datetime 和 smalldatetime。有关时间值指定的更多信息,请参见时间格式

如果只指定年份的最后两位数字,则小于或等于 two digit year cutoff 配置选项的值的最后两位数字的值所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2050(默认),则 49 被解释为 2049,50 被解释为 1950。为避免模糊,请使用四位数字的年份。

返回类型

nvarchar

例子:表中有这样几个字段,id,city,date...现在要按照星期来排列数据。如下:

  ID     city     date  
  1       a     2005/01/24  
  2       b     2005/01/23  
  3       b     2005/01/23  
  4       a     2005/01/21  
  5       d     2005/01/20  
  6       c     2005/01/19  
  7       c     2005/01/19  
  8       a     2005/01/18  
   
  排列成下面的
   
  city/day  Mon   Tue   Wed    Thu    Fri   Sat   Sun  Total  
      a          1         1        0          0        1     0       0     3  
      b          0         0        0          0        0     0       2     2  
      c          0         0        2           0       0     0       0     2  
      d          0         0        0          1        0     0       0     1  
  Total       1         1        2          1        1     0       2     8  

下面分别演示 :

 Create     Table     c(id     int,city     varchar(2),date   datetime) 

DATAPART:

  select   distinct   city   as   [city/day],  
  (select   count(*)   from   c     where   datepart(dw,date)=1   and   city=a.city)   as   mon   ,  
  (select   count(*)   from   c     where   datepart(dw,date)=2   and   city=a.city)   as   tue,  
  (select   count(*)   from   c     where   datepart(dw,date)=3   and   city=a.city)   as   wed,  
  (select   count(*)   from   c     where   datepart(dw,date)=4   and   city=a.city)   as   thr,  
  (select   count(*)   from   c     where   datepart(dw,date)=5   and   city=a.city)   as   fri,  
  (select   count(*)   from   c     where   datepart(dw,date)=6   and   city=a.city)   as   sat,  
  (select   count(*)   from   c     where   datepart(dw,date)=7   and   city=a.city)   as   sun     from   c   a  
  union   all  
  select   distinct   'total'   as   [city/day],  
  (select   count(*)   from   c   where   datepart(dw,date)=1   )   as   mon,  
  (select   count(*)   from   c   where   datepart(dw,date)=2   )   as   tue,  
  (select   count(*)   from   c   where   datepart(dw,date)=3   )   as   wed,  
  (select   count(*)   from   c   where   datepart(dw,date)=4   )   as   thr,  
  (select   count(*)   from   c   where   datepart(dw,date)=5   )   as   fri,  
  (select   count(*)   from   c   where   datepart(dw,date)=6   )   as   sat,  
  (select   count(*)   from   c   where   datepart(dw,date)=7   )   as   sun   from   c   b

DATANAME:

select distinct city as [city/day],mon=(select count(*) from c where datename(dw,date)='星期一' and city=a.city),  
  tue=(select count(*) from c where datename(dw,date)='星期二' and city=a.city),  
  wed=(select count(*) from c where datename(dw,date)='星期三' and city=a.city),  
  thu=(select count(*) from c where datename(dw,date)='星期四' and city=a.city),  
  fri=(select count(*) from c where datename(dw,date)='星期五' and city=a.city),  
  sat=(select count(*) from c where datename(dw,date)='星期六' and city=a.city),  
  sun=(select count(*) from c where datename(dw,date)='星期七' and city=a.city),  
  total=(select count(*) from c where city=a.city)  
from c a group by city    
union  all  
select distinct 'Total',mon=(select count(*) from c where datename(dw,date)='星期一'),  
  tue=(select count(*) from c where datename(dw,date)='星期二'),  
  wed=(select count(*) from c where datename(dw,date)='星期三'),  
  thu=(select count(*) from c where datename(dw,date)='星期四'),  
  fri=(select count(*) from c where datename(dw,date)='星期五'),  
  sat=(select count(*) from c where datename(dw,date)='星期六'),  
  sun=(select count(*) from c where datename(dw,date)='星期七'),  
  total=(select count(*) from c) from c  

 

---以上的例子引资csdn:http://topic.csdn.net/t/20050125/02/3751045.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值