Extracting the Date from SQL Server DateTime Values
SQL Server's GETDATE()
function provides a DateTime value containing both date and time. Often, only the date is required. This guide demonstrates how to efficiently retrieve just the date component.
SQL Query:
The most efficient method to isolate the date portion, excluding the time, uses the following query:
<code class="language-sql">SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))</code>
Example using GETDATE():
To extract the date from the current date and time:
<code class="language-sql">SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))</code>
Output:
The output is a DateTime value with the time set to midnight (00:00:00.000), effectively representing only the date:
<code>2008-09-22 00:00:00.000</code>
Key Benefits:
This approach offers several advantages:
The above is the detailed content of How to Extract Only the Date from a SQL Server DateTime Value?. For more information, please follow other related articles on the PHP Chinese website!