Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex DataLens
    • Overview
    • Formula syntax
      • All Functions
        • Overview
        • DATEADD
        • DATEPART
        • DATETRUNC
        • DAY
        • DAYOFWEEK
        • HOUR
        • MINUTE
        • MONTH
        • NOW
        • QUARTER
        • SECOND
        • TODAY
        • WEEK
        • YEAR
      • Function Availability
    • Parameters
  • Audit Trails events
  1. Calculated fields
  2. Function reference
  3. Date/Time functions
  4. DATETRUNC

DATETRUNC

Written by
Yandex Cloud
Improved by
amatol
Updated at November 6, 2024

SyntaxSyntax

DATETRUNC( datetime, unit [ , number ] )

DescriptionDescription

Rounds datetime down to the given unit. If optional number is given, then the value is rounded down to a number multiple of unit (omitting number is the same as number = 1).

Supported units:

  • "second";
  • "minute";
  • "hour";
  • "day" (acts as the day of the year if number is specified);
  • "week";
  • "month";
  • "quarter";
  • "year".

When using a function with three arguments, it is processed on the ClickHouse® side by the toStartOfInterval function. Rounding is done relative to a specific point in time, as detailed in the table in the function description. For example:

DATETRUNC(#2018-07-12 11:07:13#, "month", 4) = #2018-05-01 00:00:00#

For the unit argument set to month, rounding starts from 1900-01-01. There are 1,422 months between 2018-07-12 and 1900-01-01. Rounding this value to the nearest number divisible by 4 (the number argument), we get 1,420 months. Thus, adding 1,420 months to 1900-01-01 gives us 2018-05-01.

Argument types:

  • datetime — Date | Datetime
  • unit — String
  • number — Integer

Return type: Same type as (datetime)

Note

Only constant values are accepted for the arguments (unit, number).

Note

The function with three arguments is only available for the sources ClickHouse version 21.8 or higher.

ExamplesExamples

DATETRUNC(#2018-07-12 11:07:13#, "minute") = #2018-07-12 11:07:00#
DATETRUNC(#2018-07-12#, "year", 5) = #2015-01-01#
DATETRUNC(#2018-07-12 11:07:13#, "second", 5) = #2018-07-12 11:07:10#

Data source supportData source support

ClickHouse 21.8, Files, Google Sheets, Microsoft SQL Server 2017 (14.0), MySQL 5.7, Oracle Database 12c (12.1), PostgreSQL 9.3, Yandex Documents, YDB.

Was the article helpful?

Previous
DATEPART
Next
DAY
© 2025 Direct Cursus Technology L.L.C.