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)

解决方案 »

  1.   

    server :print convert(nvarchar(20),getdate(),120)
    access :format(date,'YYYY-MM-DD')
      

  2.   

    SQL Server:
    --有索引的话,能用到
    select * from table1 where in_time>=CONVERT(char(10),GetDate(),120) and in_time<dateadd(day,1,CONVERT(char(10),GetDate(),120))
      

  3.   

    同意思归的做法,同时也可以使用下面的语句:
    select day(getdate())
      

  4.   

    [Sql Server]
    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?
      

  5.   

    select * from table1 where (CONVERT(varchar(10),GetDate(),20)='2002-2-10'or
    select * from table1 where datediff(day,datefield,'2002-6-22')=0cast和CONVERT没什么区别,CAST是SQL-92标准里,用他是为了保持兼容性
      

  6.   


    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)
      

  7.   

    SQL Server 2000 中:
    --有索引的话,能用到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)))
      

  8.   

    --SQL Server 2000 中:
    --有索引的话,能用到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)))
      

  9.   

    to OpenVMS(半知半解),嗯,DateDiff 是好办法。不过 case / convert 的区别不是很满意。to azsoft(你行的),请下次说清楚些。to webo(提倡技术共享,提高国人水平),太罗嗦了!这么麻烦,能确保效率高吗?to foolishchao(亚超),谢谢你又给了这么多方法,不过我感觉实用的不多。可能我理解不了吧。剩下的还是我自己来吧。
    OK,结贴。