日期:2014-05-17 浏览次数:20449 次
use master
go
create database test
go
use test
go
if exists(select 1 from sys.objects where name='tDateTime' and type='U')
drop table tDateTime
go
create table tDateTime --表名
(
id int identity primary key,
Title nvarchar(50),
StartTime datetime,
EndTime datetime
)
insert into tDateTime values('aaa','2012-08-08 00:00:00.000','2013-01-26 00:00:00.000')
insert into tDateTime values('bbb','2012-10-17 00:00:00.000','2012-10-24 00:00:00.000')
insert into tDateTime values('ccc','2012-10-17 00:00:00.000','2012-10-24 00:00:00.000')
insert into tDateTime values('ddd','2012-10-31 00:00:00.000','2012-11-07 00:00:00.000')
insert into tDateTime values('ddd','2012-09-01 00:00:00.000','2012-12-09 00:00:00.000')
insert into tDateTime values('ccc','2012-12-05 00:00:00.000','2012-12-20 00:00:00.000')
insert into tDateTime values('bbb','2012-09-03 00:00:00.000','2013-05-12 00:00:00.000')
select * from tDateTime
go
select id,Title,StartTime,EndTime,
CONVERT(VARCHAR(4),StartTime,23) yearStart,
CONVERT(VARCHAR(4),EndTime,23) yearEnd,
CONVERT(VARCHAR(2),StartTime,10) monthStart,
CONVERT(VARCHAR(2),EndTime,10) monthEnd,
CONVERT(VARCHAR(2),StartTime,5) dayStart,
CONVERT(VARCHAR(2),EndTime,5) dayEnd,
datepart(dd,dateadd(mm,1,convert(char(8),CONVERT(datetime,'2012-12-01',120),21)+'01')-1) as Diff --当月天数
from tDateTime where 1=1 and '2012-12' between CONVERT(VARCHAR(7),StartTime,23) and CONVERT(VARCHAR(7),EndTime,23)
--其中'2012-12-01'和'2012-12'为同一个参数。(日期控件显示的年和月)