首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎样根据日期推算出星期?该如何解决

2012-04-23 
怎样根据日期推算出星期?原数据表是这样:create table t(日期datetime)insert into t select 2011-8-11 0

怎样根据日期推算出星期?
原数据表是这样:



create table t

(

日期 datetime

)
insert into t select '2011-8-11 01:50:00'

insert into t select '2011-9-17 02:03:00'

insert into t select '2012-4-3 08:01:00'

drop table t

我想要得到这样的结果


日期 星期

2011-8-11 星期四

2011-9-17 星期六

2012-4-3 星期二

[解决办法]

SQL code
set datefirst 1select date,(case datepart(weekday,date) when 1 then '星期一'                                         when 2 then ''                                         when 3 then ''                                         ...                                         when 7 then '星期日' else null end) 星期from tb
[解决办法]
SQL code
set datefirst 1select convert(varchar(10),getdate(),120) + ' 星期' + convert(varchar,datepart(dw,getdate()))/*---------------------------------------------2012-03-09 星期5(1 行受影响)*/ 

热点排行