当我选择日期在SQL返回为2011-02-25 21:17:33.933。但我只需要日期部分,即2011-02-25。我该怎么做呢?


当前回答

在转换为日期后将其转换回datetime,以便在需要时保持相同的datatime

select Convert(datetime, Convert(date, getdate())  )

其他回答

使用很简单:

convert(date, Btch_Time)

在下面的例子:

表:

Efft_d       Loan_I  Loan_Purp_Type_C   Orig_LTV    Curr_LTV    Schd_LTV    Un_drwn_Bal_a      Btch_Time            Strm_I  Btch_Ins_I
2014-05-31  200312500   HL03             NULL         1.0000    1.0000         1.0000      2014-06-17 11:10:57.330  1005    24851e0a-53983699-14b4-69109

Select * from helios.dbo.CBA_SRD_Loan where Loan_I in ('200312500') and convert(date, Btch_Time) = '2014-06-17'

我猜他想要根绳子。

select convert(varchar(10), '2011-02-25 21:17:33.933', 120)

这里的120告诉转换函数,我们以以下格式传递输入日期:yyyy-mm-dd hh:mi:ss。

在转换为日期后将其转换回datetime,以便在需要时保持相同的datatime

select Convert(datetime, Convert(date, getdate())  )

select DATE(字段)

字段值:2020-12-15 12:19:00

选择值:2020-12-15

最快的是date ediff。

select dateadd(d, datediff(d,0, [datecolumn]), 0), other..
from tbl

但是如果你只需要使用这个值,那么你可以跳过dateadd,例如。

select ...
WHERE somedate <= datediff(d, 0, getdate())

表达式datediff(d, 0, getdate())足以返回今天的日期而不包含时间部分。