Home > Database > Mysql Tutorial > How to Truncate a SQL Server DateTime to Date?

How to Truncate a SQL Server DateTime to Date?

Patricia Arquette
Release: 2025-01-23 07:01:09
Original
525 people have browsed it

How to Truncate a SQL Server DateTime to Date?

SQL Server DateTime truncated to Date

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>
Copy after login

For example, setting @your_date to GETDATE() and executing the query will produce:

<code>2008-09-22 00:00:00.000</code>
Copy after login

Advantages:

  • Avoid explicit conversions between varchar and datetime data types.
  • No need to consider locale settings.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template