| 订阅 | 在线投稿
分享
 
 
 

初学者必读:精讲SQL中的时间计算语句

2008-08-15 06:29:25 编辑來源:互联网 国际版 评论
 
 
本文为【初学者必读:精讲SQL中的时间计算语句】的汉字拼音对照版显示拼音
  wenqingwenruhejisuanyigebiaozhongdezhouqishihejiezhiriqibingxiedaobiaoziduanzhong woyaocongyigebiaoxianglingyigebiaodaorushujubingjinxingzhuanhuanyongdeshiVB

  woxianzaiyouyouyigebiao zhuyaoziduanyou

  time_id int

  time_date datetime

  year int

  week_of_year int

  day nvarhar

  xiangyaozhuanhuanchenglingwaiyizhangbiao

  time_id int

  time_date datetime

  year int

  week_of_year nvarchar

  yuanlaidebiaoneirongshi

  --------------------

  1 2003-07-09 2003 20 xingqiri

  1 2003-07-10 2003 20 xingqiyi

  1 2003-07-11 2003 20 xingqier

  xiangyaobiancheng

  --------------------

  1 07/09/2003 2003 di20zhou7/9-7/17

  1 07/10/2003 2003 di20zhou7/9-7/17

  1 07/11/2003 2003 di20zhou7/9-7/17

  qingwenzhegeyujuyinggaizenmequxie

  da if object_id('tablename') is not null drop table tablename

  select 1 as time_id, '2003-07-09' as time_date, 2003 as [year], 20 as week_of_year, 'xingqiri' as [day]

  into tablename

  union select 1, '2003-07-10', 2003, 20, 'xingqiyi'

  union select 1, '2003-07-11', 2003, 20, 'xingqier'

  ------------------------------------------------

  select time_id, time_date, [year], 'di' + cast(week_of_year as varchar(2)) + 'zhou'

  + cast(month(week_begin) as varchar(2)) + '/' + cast(day(week_begin) as varchar(2)) + '-'

  + cast(month(week_end) as varchar(2)) + '/' + cast(day(week_end) as varchar(2)) as week_of_year

  from (select *, dateadd(day, 1 - datepart(weekday, time_date), time_date) as week_begin,

  dateadd(day, 7 - datepart(weekday, time_date), time_date) as week_end from tablename) a

  /*

  time_id time_date year week_of_year

  1 2003-07-09 2003 di20zhou7/6-7/12

  1 2003-07-10 2003 di20zhou7/6-7/12

  1 2003-07-11 2003 di20zhou7/6-7/12

  */

  ------------------------------------------------

  drop table tablename

  wentisuiranjiejueledanzhegelizibingbujubeitongyongxinghuanshigeansuoyiwomenfenxilenidedaimafaxianyigewentiriqifanweishiruhequedingdesuoyiwomenbatayanshenfasandaonengfouzizhushedingriqidefanweinebirushedingdaoxingqiyihuoxingqitiankaishi

  silu

  SET DATEFIRST

  jiangyizhoudediyitianshezhiweicong 1 dao 7 zhijiandeyigeshuzi

  yufa

  SET DATEFIRST { number | @number_var }

  shenshu

  number | @number_var

  shiyigezhengshubiaoshiyizhoudediyitiankeyishixialiezhizhongdeyige

  zhi yizhoudediyitianshi

  1 xingqiyi

  2 xingqier

  3 xingqisan

  4 xingqisi

  5 xingqiwu

  6 xingqiliu

  7morenzhimeiguoyingyu xingqiri

  zhushi

  使shiyong @@DATEFIRST hanshujiancha SET DATEFIRST dedangqianshezhi

  SET DATEFIRST deshezhishizaizhixinghuoyunxingshishezhierbushizaifenxishishezhi

  fangfayi

  xianchuangjianyigeyonghuhanshugenjuriqidedaozhoukaishiriqihejieshuriqi

  create function getwstend(@resultdate datetime)

  returns varchar(20)

  As

  begin

  declare @weekstart datetime,@weekend datetime,@dayweek int,@weeknum int

  set @weeknum = datepart(wk,@resultdate)

  set @dayweek = datepart(dw,@resultdate)

  begin

  set @weekstart = dateadd(dd,1-@dayweek,@resultdate)

  set @weekend = DateAdd(dd,7-@dayweek,@resultdate)

  end

  return 'di'+Convert(varchar,@weeknum)+'zhou'+'('+right(convert(varchar(10),@weekstart,111),5)+'--'+right(Convert(varchar(10),@weekend,111),5)+')'

  end

  Go

  liru

  select test.getwstend('2006-4-17')

  fanhuijieguoshidi16zhou(04/16--04/22)

  liyongzhegehanshuxiangxinjiukeyishixiannideyaoqiulebasuoyoubiao1zhongdeshujuzhuandaobiao2

  fangfaer

  weilebimianshujuderongyujianyibuyaobashujucongdiyigebiaozhuandaodiergebiaoyinweizheyanghuizaochengshujurongyunikeyizaichaxunshihou使shiyonghanshuchanshengweek_of_year nvarchar zhegelanwei

  fangfasan

  chuangjianyigebiaolanweiwei

  create table tw

  (

  time_date datetime,

  week_of_year nvarchar

  )

  ranhou1910.1.1---2050.12.31zhijiandesuoyouriqidouchanshengyigeduizhaodeweek_of_year nvarcharfangfayeshi使shiyongshangmianchuangjiandehanshuranhouchaxundeshihouzhiyaolianggebiaozuoguanlianjiukeyile

  ruguoyuanshibiaozhongdejiluhenduoerqieriqiyouhenduochongfuwomengengtuijian使shiyongfangfasan原文
 
 
问:请问,如何计算一个表中的周起始和截止日期并写到表字段中? 我要从一个表向另一个表导入数据,并进行转换,用的是VB 。 我现在有有一个表 主要字段有 time_id int time_date datetime year int week_of_year int day nvarhar 想要转换成另外一张表 time_id int time_date datetime year int week_of_year nvarchar 原来的表内容是 -------------------- 1 2003-07-09 2003 20 星期日 1 2003-07-10 2003 20 星期一 1 2003-07-11 2003 20 星期二 想要变成 -------------------- 1 07/09/2003 2003 第20周(7/9-7/17) 1 07/10/2003 2003 第20周(7/9-7/17) 1 07/11/2003 2003 第20周(7/9-7/17) 请问:这个语句应该怎么去写? 答: if object_id('tablename') is not null drop table tablename select 1 as time_id, '2003-07-09' as time_date, 2003 as [year], 20 as week_of_year, '星期日' as [day] into tablename union select 1, '2003-07-10', 2003, 20, '星期一' union select 1, '2003-07-11', 2003, 20, '星期二' ------------------------------------------------ select time_id, time_date, [year], '第' + cast(week_of_year as varchar(2)) + '周(' + cast(month(week_begin) as varchar(2)) + '/' + cast(day(week_begin) as varchar(2)) + '-' + cast(month(week_end) as varchar(2)) + '/' + cast(day(week_end) as varchar(2)) as week_of_year from (select *, dateadd(day, 1 - datepart(weekday, time_date), time_date) as week_begin, dateadd(day, 7 - datepart(weekday, time_date), time_date) as week_end from tablename) a /* time_id time_date year week_of_year 1 2003-07-09 2003 第20周(7/6-7/12) 1 2003-07-10 2003 第20周(7/6-7/12) 1 2003-07-11 2003 第20周(7/6-7/12) */ ------------------------------------------------ drop table tablename 问题虽然解决了,但这个例子并不具备通用性,还是个案,所以我们分析了你的代码,发现一个问题:日期范围是如何确定的?所以,我们把它延伸发散到:能否自主设定日期的范围呢?比如设定到星期一或星期天开始: 思路: SET DATEFIRST 将一周的第一天设置为从 1 到 7 之间的一个数字。 语法 SET DATEFIRST { number | @number_var } 参数 number | @number_var 是一个整数,表示一周的第一天,可以是下列值中的一个。 值 一周的第一天是 1 星期一 2 星期二 3 星期三 4 星期四 5 星期五 6 星期六 7(默认值,美国英语) 星期日 注释 使用 @@DATEFIRST 函数检查 SET DATEFIRST 的当前设置。 SET DATEFIRST 的设置是在执行或运行时设置,而不是在分析时设置。 方法一: 先创建一个用户函数,根据日期得到周开始日期和结束日期 create function getwstend(@resultdate datetime) returns varchar(20) As begin declare @weekstart datetime,@weekend datetime,@dayweek int,@weeknum int set @weeknum = datepart(wk,@resultdate) set @dayweek = datepart(dw,@resultdate) begin set @weekstart = dateadd(dd,1-@dayweek,@resultdate) set @weekend = DateAdd(dd,7-@dayweek,@resultdate) end return '第'+Convert(varchar,@weeknum)+'周'+'('+right(convert(varchar(10),@weekstart,111),5)+'--'+right(Convert(varchar(10),@weekend,111),5)+')' end Go 例如: select test.getwstend('2006-4-17') 返回结果是:第16周(04/16--04/22) 利用这个函数相信就可以实现你的要求了,把所有表1中的数据转到表2 方法二: 为了避免数据的冗余,建议不要把数据从第一个表转到第二个表。因为这样会造成数据冗余。你可以在查询时候使用函数产生week_of_year nvarchar 这个栏位。 方法三: 创建一个表,栏位为: create table tw ( time_date datetime, week_of_year nvarchar ) 然后1910.1.1---2050.12.31之间的所有日期都产生一个对照的week_of_year nvarchar,方法也是使用上面创建的函数。然后查询的时候。只要两个表作关联就可以了 如果原始表中的记录很多。而且日期有很多重复,我们更推荐使用方法三。
󰈣󰈤
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有