Extracting the Date from SQL Server's DateTime Data Type
Database queries often require isolating the date from a SQL Server DateTime field, leaving out the time. Several techniques can accomplish this.
One efficient approach uses the DATEADD
and DATEDIFF
functions:
<code class="language-sql">SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))</code>
For example:
<code class="language-sql">SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))</code>
This returns:
<code>2008-09-22 00:00:00.000</code>
This method offers key benefits:
varchar
to datetime
.DATEADD
function ensures consistent results regardless of locale settings.The above is the detailed content of How to Extract Only the Date from a SQL Server DateTime Datatype?. For more information, please follow other related articles on the PHP Chinese website!