When processing SQL Server date and time data, it is often necessary to extract only the date part, excluding the time. This question seeks guidance on how to achieve this using a specific example.
The provided query SELECT GETDATE()
returns a DATETIME value containing date and time components. The desired output is to get the date part as <code>2008-09-22 00:00:00.000</code>.
Solution:
The proposed solution utilizes the DATEDIFF()
and DATEADD()
functions to achieve the desired result. The following query accomplishes this task:
<code class="language-sql">SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))</code>
For example, setting @your_date
to GETDATE()
and executing the query will produce:
<code>2008-09-22 00:00:00.000</code>
Advantages:
The above is the detailed content of How to Truncate a SQL Server DateTime to Date?. For more information, please follow other related articles on the PHP Chinese website!