How to select date without time in SQL
When I select date in SQL it is returned as 2011-02-25 21:17:33.933
. But I need only the Date part, that is 2011-02-25
. How can I do this?
我猜他想要一个字符串
select convert(varchar(10), '2011-02-25 21:17:33.933', 120)
Convert(date, getdate())
请参阅https://docs.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql
The fastest is datediff
, eg
select dateadd(d, datediff(d,0, [datecolumn]), 0), other..
from tbl
But if you only need to use the value, then you can skip the dateadd, eg
select ...
WHERE somedate <= datediff(d, 0, getdate())
where the expression datediff(d, 0, getdate())
is sufficient to return today's date without time portion.
上一篇: SQL Server中奇怪的日期时间行为
下一篇: 如何在SQL中选择没有时间的日期