{"id":2707,"date":"2025-01-20T23:12:54","date_gmt":"2025-01-21T06:12:54","guid":{"rendered":"https:\/\/www.sqltutorial.org\/?page_id=2707"},"modified":"2025-01-20T23:19:26","modified_gmt":"2025-01-21T06:19:26","slug":"sql-date_trunc","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/","title":{"rendered":"SQL DATE_TRUNC Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to use the SQL <code>DATE_TRUNC<\/code> function to truncate a date, time, or timestamp.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-date_trunc-function'>Introduction to SQL DATE_TRUNC Function <a href=\"#introduction-to-sql-date_trunc-function\" class=\"anchor\" id=\"introduction-to-sql-date_trunc-function\" title=\"Anchor for Introduction to SQL DATE_TRUNC Function\">#<\/a><\/h2>\n\n\n\n<p>The <code>DATE_TRUNC<\/code> function allows you to truncate a date, time, or timestamp to a specified precision.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>DATE_TRUNC<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">DATE_TRUNC(unit, <span class=\"hljs-type\">date<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>unit<\/code>: The unit of time you want to truncate the date. The unit can be <code>YEAR<\/code>, <code>MONTH<\/code>, <code>DAY<\/code>, <code>HOUR<\/code>, <code>MINUTE<\/code>, <code>SECOND<\/code>, etc.<\/li>\n\n\n\n<li><code>date<\/code>: The date, time, or timestamp you want to truncate.<\/li>\n<\/ul>\n\n\n\n<p>The <code>DATE_TRUNC<\/code> function can be helpful for time series analysis and data aggregation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-date_trunc-function-examples'>SQL DATE_TRUNC function examples <a href=\"#sql-date_trunc-function-examples\" class=\"anchor\" id=\"sql-date_trunc-function-examples\" title=\"Anchor for SQL DATE_TRUNC function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s use the <code>employees<\/code> table from the <a href=\"https:\/\/www.sqltutorial.org\/sql-sample-database\/\">HR sample database<\/a> to demonstrate the <code>DATE_TRUNC<\/code> function:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"169\" height=\"273\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\" alt=\"SQL DATE_TRUNC Function - The employees Table \" class=\"wp-image-237\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='truncating-to-the-start-of-the-year'>Truncating to the Start of the Year <a href=\"#truncating-to-the-start-of-the-year\" class=\"anchor\" id=\"truncating-to-the-start-of-the-year\" title=\"Anchor for Truncating to the Start of the Year\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>DATE_TRUNC<\/code> function to truncate values in the <code>hire_date<\/code> column of employees to the start of the year:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  first_name,\n  last_name,\n  hire_date,\n  DATE_TRUNC(<span class=\"hljs-string\">'YEAR'<\/span>, hire_date) <span class=\"hljs-keyword\">AS<\/span> hire_year_start\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_year_start;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGVtcGxveWVlX2lkLCBmaXJzdF9uYW1lLCBsYXN0X25hbWUsIGhpcmVfZGF0ZSwgREFURV9UUlVOQygnWUVBUicsIGhpcmVfZGF0ZSkgQVMgaGlyZV95ZWFyX3N0YXJ0IEZST00gZW1wbG95ZWVzIE9SREVSIEJZIGhpcmVfeWVhcl9zdGFydDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> employee_id | first_name  |  last_name  | hire_date  | hire_year_start\n<span class=\"hljs-comment\">-------------+-------------+-------------+------------+-----------------<\/span>\n         <span class=\"hljs-number\">200<\/span> | Jennifer    | Whalen      | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-17<\/span> | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">100<\/span> | Steven      | King        | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-06<\/span><span class=\"hljs-number\">-17<\/span> | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">101<\/span> | Neena       | Kochhar     | <span class=\"hljs-number\">1989<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-21<\/span> | <span class=\"hljs-number\">1989<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">103<\/span> | Alexander   | Hunold      | <span class=\"hljs-number\">1990<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-03<\/span> | <span class=\"hljs-number\">1990<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query returns the employee ID, first name, last name, original hire date, and the start of the year of the hire date:<\/p>\n\n\n\n<p>You can use the data from the <code>hire_year_start<\/code> column to calculate the headcount at the beginning of each year.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='grouping-employees-by-the-starting-of-the-year'>Grouping Employees by the Starting of the Year <a href=\"#grouping-employees-by-the-starting-of-the-year\" class=\"anchor\" id=\"grouping-employees-by-the-starting-of-the-year\" title=\"Anchor for Grouping Employees by the Starting of the Year\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>DATE_TRUNC<\/code> function to truncate the hire datethe&nbsp;&nbsp;to start of the year and uses the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-group-by\/\">GROUP BY<\/a><\/code> clause with the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-count\/\">COUNT<\/a><\/code> function to calculate headcount for each start of year:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  DATE_TRUNC(<span class=\"hljs-string\">'YEAR'<\/span>, hire_date) <span class=\"hljs-keyword\">AS<\/span> hire_year_start,\n  COUNT(*) <span class=\"hljs-keyword\">AS<\/span> headcount\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_year_start\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_year_start;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIERBVEVfVFJVTkMoJ1lFQVInLCBoaXJlX2RhdGUpIEFTIGhpcmVfeWVhcl9zdGFydCwgQ09VTlQoKikgQVMgaGVhZGNvdW50IEZST00gZW1wbG95ZWVzIEdST1VQIEJZIGhpcmVfeWVhcl9zdGFydCBPUkRFUiBCWSBoaXJlX3llYXJfc3RhcnQ7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> hire_year_start | headcount\n<span class=\"hljs-comment\">-----------------+-----------<\/span>\n <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">2<\/span>\n <span class=\"hljs-number\">1989<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1990<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1991<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1993<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1994<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">7<\/span>\n <span class=\"hljs-number\">1995<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">2<\/span>\n <span class=\"hljs-number\">1996<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">4<\/span>\n <span class=\"hljs-number\">1997<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |        <span class=\"hljs-number\">10<\/span>\n <span class=\"hljs-number\">1998<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">6<\/span>\n <span class=\"hljs-number\">1999<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">4<\/span>\n <span class=\"hljs-number\">2000<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>      |         <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='truncating-to-the-start-of-the-month'>Truncating to the Start of the Month <a href=\"#truncating-to-the-start-of-the-month\" class=\"anchor\" id=\"truncating-to-the-start-of-the-month\" title=\"Anchor for Truncating to the Start of the Month\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>DATE_TRUNC<\/code> function to truncate the <code>hire_date<\/code> to the start of the month:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  first_name,\n  last_name,\n  hire_date,\n  DATE_TRUNC(<span class=\"hljs-string\">'MONTH'<\/span>, hire_date) <span class=\"hljs-keyword\">AS<\/span> hire_month_start\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_month_start;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGVtcGxveWVlX2lkLCBmaXJzdF9uYW1lLCBsYXN0X25hbWUsIGhpcmVfZGF0ZSwgREFURV9UUlVOQygnTU9OVEgnLCBoaXJlX2RhdGUpIEFTIGhpcmVfbW9udGhfc3RhcnQgRlJPTSBlbXBsb3llZXMgT1JERVIgQlkgaGlyZV9tb250aF9zdGFydDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">employee_id | first_name  |  last_name  | hire_date  | hire_month_start\n<span class=\"hljs-comment\">-------------+-------------+-------------+------------+------------------<\/span>\n         <span class=\"hljs-number\">100<\/span> | Steven      | King        | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-06<\/span><span class=\"hljs-number\">-17<\/span> | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-06<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">200<\/span> | Jennifer    | Whalen      | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-17<\/span> | <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">101<\/span> | Neena       | Kochhar     | <span class=\"hljs-number\">1989<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-21<\/span> | <span class=\"hljs-number\">1989<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">103<\/span> | Alexander   | Hunold      | <span class=\"hljs-number\">1990<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-03<\/span> | <span class=\"hljs-number\">1990<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>\n         <span class=\"hljs-number\">104<\/span> | Bruce       | Ernst       | <span class=\"hljs-number\">1991<\/span><span class=\"hljs-number\">-05<\/span><span class=\"hljs-number\">-21<\/span> | <span class=\"hljs-number\">1991<\/span><span class=\"hljs-number\">-05<\/span><span class=\"hljs-number\">-01<\/span>\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query returns the employee ID, first name, last name, original hire date, and the start of the month of the hire date:<\/p>\n\n\n\n<p>You can use the data in the <code>hire_month_start<\/code> for calculating the number of new employees on payroll for starting from the beginning of the hire month.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='grouping-employees-by-hire-month'>Grouping Employees by Hire Month <a href=\"#grouping-employees-by-hire-month\" class=\"anchor\" id=\"grouping-employees-by-hire-month\" title=\"Anchor for Grouping Employees by Hire Month\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>DATE_TRUNC<\/code> to count employees by the start of the hired month:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  DATE_TRUNC(<span class=\"hljs-string\">'MONTH'<\/span>, hire_date)::<span class=\"hljs-type\">DATE<\/span> <span class=\"hljs-keyword\">AS<\/span> hire_month,\n  COUNT(*) <span class=\"hljs-keyword\">AS<\/span> new_employee_count\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_month\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_month;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIERBVEVfVFJVTkMoJ01PTlRIJywgaGlyZV9kYXRlKTo6REFURSBBUyBoaXJlX21vbnRoLCBDT1VOVCgqKSBBUyBuZXdfZW1wbG95ZWVfY291bnQgRlJPTSBlbXBsb3llZXMgR1JPVVAgQlkgaGlyZV9tb250aCBPUkRFUiBCWSBoaXJlX21vbnRoOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> hire_month | new_employee_count\n<span class=\"hljs-comment\">------------+--------------------<\/span>\n <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-06<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1987<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1989<\/span><span class=\"hljs-number\">-09<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1990<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1991<\/span><span class=\"hljs-number\">-05<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1993<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-number\">1994<\/span><span class=\"hljs-number\">-06<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">4<\/span>\n <span class=\"hljs-number\">1994<\/span><span class=\"hljs-number\">-08<\/span><span class=\"hljs-number\">-01<\/span> |                  <span class=\"hljs-number\">2<\/span>\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the SQL <code>DATE_TRUNC<\/code> function for truncating dates and times to a specified precision.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2707\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\"\n\t\t\t\tdata-post-title=\"SQL DATE_TRUNC Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2707\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\"\n\t\t\t\tdata-post-title=\"SQL DATE_TRUNC Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In this tutorial, you&#8217;ll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp. Introduction to SQL DATE_TRUNC Function # The DATE_TRUNC function allows you to truncate a date, time, or timestamp to a specified precision. Here&#8217;s the syntax of the DATE_TRUNC function: In this syntax: The DATE_TRUNC function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1498,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2707","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL DATE_TRUNC Function<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL DATE_TRUNC Function\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-21T06:19:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\" \/>\n\t<meta property=\"og:image:width\" content=\"169\" \/>\n\t<meta property=\"og:image:height\" content=\"273\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\",\"name\":\"SQL DATE_TRUNC Function\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"datePublished\":\"2025-01-21T06:12:54+00:00\",\"dateModified\":\"2025-01-21T06:19:26+00:00\",\"description\":\"In this tutorial, you'll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#primaryimage\",\"url\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"contentUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"width\":169,\"height\":273,\"caption\":\"SQL Correlated Subquery - employees table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Date Functions\",\"item\":\"https:\/\/www.sqltutorial.org\/sql-date-functions\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL DATE_TRUNC Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL DATE_TRUNC Function","description":"In this tutorial, you'll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/","og_locale":"en_US","og_type":"article","og_title":"SQL DATE_TRUNC Function","og_description":"In this tutorial, you'll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp.","og_url":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-01-21T06:19:26+00:00","og_image":[{"width":169,"height":273,"url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/","url":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/","name":"SQL DATE_TRUNC Function","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#primaryimage"},"image":{"@id":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","datePublished":"2025-01-21T06:12:54+00:00","dateModified":"2025-01-21T06:19:26+00:00","description":"In this tutorial, you'll learn how to use the SQL DATE_TRUNC function to truncate a date, time, or timestamp.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#primaryimage","url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","contentUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","width":169,"height":273,"caption":"SQL Correlated Subquery - employees table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-date-functions\/sql-date_trunc\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL Date Functions","item":"https:\/\/www.sqltutorial.org\/sql-date-functions\/"},{"@type":"ListItem","position":3,"name":"SQL DATE_TRUNC Function"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/2707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=2707"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/2707\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/1498"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=2707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}