用户操作
[即时聊天] [发私信] [加为好友]
石頭ID:stonezhuzisgu
19290次访问,排名6145好友1人,关注者2
stonezhuzisgu的文章
原创 18 篇
翻译 1 篇
转载 47 篇
评论 7 篇
最近评论
KF252895278:精彩
liulogo:初次接触 大致知道怎么回事儿啦 谢谢楼主先
zhoushengzong:不错,讲地很透彻!
xin_cdm:讲的很清楚 谢谢了
agjyfm:wow gold
文章分类
收藏
    相册
    AdvanJS
    JavaScript language advanced Tips & Tricks
    收录的正则表达式
    ASP.net
    codeproject---.net
    dotNet男孩社区
    孟宪e章
    杏花博客
    friends
    仔仔欣
    InfoQ
    InfoQ:时刻关注企业软件开发领域的变化与创新
    Some blog Link
    aimingoo的专栏
    Johnson(大可山)的专栏[十年一日,专注图形图像]
    Ray's blog
    visual studio 博客园
    yangshu的专栏--音频、视频知识小站
    叶帆工作室
    大坡3D软件开发
    孟子E章
    胡长城(银狐999)BLOG
    蝈蝈俊.net
    西洋樱草的专栏
    贝高林的Blog--关注Web Service,关注Web2.0,关注TOM-Skype技术
    邹建
    语言学习--C
    Aaron25的专栏
    Allegro游戏图形库
    dev-c
    Goncely的专栏
    paobo的专栏
    中国it实验室
    人月
    天极网
    编程中国
    蓝色梦想的程序人生--c-java
    阿来的专栏--linux
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 MS SQL datepart,datename收藏

    新一篇: SQL Group by | 旧一篇: NHibernate的初学几点错误记录

    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

    发表于 @ 2007年11月04日 15:33:00|评论(loading...)|编辑

    新一篇: SQL Group by | 旧一篇: NHibernate的初学几点错误记录

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © stonezhuzisgu