using DatePart
DATEPART(year, GETDATE())
DATEPART(month, GETDATE())
DATEPART(day, GETDATE()) or you can use CONVERT function in SQL Server:
select CONVERT(varchar(10),GetDate(),120)
DATEPART(year, GETDATE())
DATEPART(month, GETDATE())
DATEPART(day, GETDATE()) or you can use CONVERT function in SQL Server:
select CONVERT(varchar(10),GetDate(),120)
access :format(date,'YYYY-MM-DD')
--有索引的话,能用到
select * from table1 where in_time>=CONVERT(char(10),GetDate(),120) and in_time<dateadd(day,1,CONVERT(char(10),GetDate(),120))
select day(getdate())
convert 的方法中,我感觉 char(10) 的效率比 varchar & nvarchar 都高(自我感觉),这个问题谢谢 foolishchao(亚超) 了。[Access]
谢谢 mazhayang(蚂蚱先生),解决了。to saucer(思归),谢谢你补充 DatePart,这个我知道,只是觉得用在这里效率太低,不知道你的看法?同样谢谢你第一个告诉我用 convert。to zhujiechang(小朱),你的方法未免太麻烦了。to stonegem(啥都不懂,啥都想学),你的方法只能获得日期,而丢掉了年和月。[其他疑问]
有没有比 convert()(access/Format())效率更高的办法???就是直接用一个函数获得时间的日期部分?我以前看过一个用 {d'2002-6-20'} 这种格式表示日期的,忘了在那里看到的,各位能给解释一下吗?
还有,convert 和 case 有什么区别吗?为什么都推荐用 convert?
select * from table1 where datediff(day,datefield,'2002-6-22')=0cast和CONVERT没什么区别,CAST是SQL-92标准里,用他是为了保持兼容性
declare @temp_date smalldatetime
declare @temp_month char(2)
declare @temp_day char(2)set @temp_month=convert(char(2),month(getdate()))
set @temp_day=convert(char(2),day(getdate()))if len(@temp_month)=1 set @temp_month='0'+@temp_month
if len(@temp_day)=1 set @temp_day='0'+@temp_dayset @temp_date=convert(smalldatetime,convert(char(4),year(getdate()))+@temp_month+@temp_day)
--有索引的话,能用到select * from table1
where in_time>=convert(int,getdate()-0.5) and in_time<convert(int,getdate()+0.5)
select * from table1
where in_time>=convert(datetime,convert(int,getdate()-0.5)) and in_time<convert(datetime,convert(int,getdate()+0.5))select * from table1
where in_time>=floor(convert(float,getdate())) and in_time<floor(convert(float,getdate()+1))
select * from table1
where in_time>=convert(datetime,floor(convert(float,getdate()))) and in_time<convert(datetime,floor(convert(float,getdate()+1)))
--有索引的话,能用到select * from table1
where in_time>=convert(int,getdate()-0.5)
and in_time<convert(int,getdate()+0.5)
--或
select * from table1
where in_time>=convert(datetime,convert(int,getdate()-0.5))
and in_time<convert(datetime,convert(int,getdate()+0.5))
--或
select * from table1
where in_time>=floor(convert(float,getdate()))
and in_time<floor(convert(float,getdate()+1))
--或
select * from table1
where in_time>=convert(datetime,floor(convert(float,getdate())))
and in_time<convert(datetime,floor(convert(float,getdate()+1)))
OK,结贴。