扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:IT 专家网 来源:IT 专家网 2007年9月17日
问:请问专家,如何计算一个表中的周起始和截止日期并写到表字段中? 我要从一个表向另一个表导入数据,并进行转换,用的是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
问:请问专家,如何计算一个表中的周起始和截止日期并写到表字段中? 我要从一个表向另一个表导入数据,并进行转换,用的是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
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者