You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<1> string expression denoting the unit to which the date/datetime should be truncated to
509
-
<2> date/datetime expression
508
+
<1> string expression denoting the unit to which the date/datetime/interval should be truncated to
509
+
<2> date/datetime/interval expression
510
510
511
-
*Output*: datetime
511
+
*Output*: datetime/interval
512
512
513
-
*Description*: Truncate the date/datetime to the specified unit by setting all fields that are less significant than the specified
513
+
*Description*: Truncate the date/datetime/interval to the specified unit by setting all fields that are less significant than the specified
514
514
one to zero (or one, for day, day of week and month). If any of the two arguments is `null` a `null` is returned.
515
+
If the first argument is `week` and the second argument is of `interval` type, an error is thrown since the `interval` data type doesn't support a `week` time unit.
SELECT DATE_TRUNC('hour', INTERVAL '1 12:43:21' DAY TO SECONDS) as dt_hour, DATE_TRUNC('minute', INTERVAL '1 12:43:21' DAY TO SECONDS) as dt_min,
504
+
DATE_TRUNC('seconds', INTERVAL '1 12:43:21' DAY TO SECONDS) as dt_sec, DATE_TRUNC('ms', INTERVAL '1 12:43:21' DAY TO SECONDS)::string as dt_millis,
505
+
DATE_TRUNC('mcs', INTERVAL '1 12:43:21' DAY TO SECONDS)::string as dt_micro, DATE_TRUNC('nanoseconds', INTERVAL '1 12:43:21' DAY TO SECONDS)::string as dt_nano;
Georgi | 2286-06-26 00:00:00.000Z | 1986-06-26T00:00:00.000Z | 2
611
+
;
612
+
588
613
dateTruncFilter
589
614
schema::emp_no:i|hire_date:ts|dt:ts
590
615
SELECT emp_no, hire_date, DATE_TRUNC('quarter', hire_date) as dt FROM test_emp WHERE DATE_TRUNC('quarter', hire_date) > '1994-07-01T00:00:00.000Z'::timestamp ORDER BY emp_no;
@@ -601,6 +626,24 @@ SELECT emp_no, hire_date, DATE_TRUNC('quarter', hire_date) as dt FROM test_emp W
SELECT first_name, hire_date FROM test_emp WHERE hire_date > '2090-03-05T10:11:22.123Z'::datetime - DATE_TRUNC('centuries', INTERVAL 190 YEARS) ORDER BY first_name DESC, hire_date ASC LIMIT 10;
632
+
633
+
first_name | hire_date
634
+
---------------+-------------------------
635
+
null | 1990-06-20 00:00:00.000Z
636
+
null | 1990-12-05 00:00:00.000Z
637
+
null | 1991-09-01 00:00:00.000Z
638
+
null | 1992-01-03 00:00:00.000Z
639
+
null | 1994-02-17 00:00:00.000Z
640
+
Yongqiao | 1995-03-20 00:00:00.000Z
641
+
Yishay | 1990-10-20 00:00:00.000Z
642
+
Yinghua | 1990-12-25 00:00:00.000Z
643
+
Weiyi | 1993-02-14 00:00:00.000Z
644
+
Tuval | 1995-12-15 00:00:00.000Z
645
+
;
646
+
604
647
dateTruncGroupBy
605
648
schema::count:l|dt:ts
606
649
SELECT count(*) as count, DATE_TRUNC('decade', hire_date) dt FROM test_emp GROUP BY dt ORDER BY 2;
@@ -611,6 +654,24 @@ SELECT count(*) as count, DATE_TRUNC('decade', hire_date) dt FROM test_emp GROUP
611
654
41 | 1990-01-01 00:00:00.000Z
612
655
;
613
656
657
+
dateTruncGroupByWithInterval
658
+
schema::count:l|dt:ts
659
+
SELECT count(*) as count, birth_date + DATE_TRUNC('hour', INTERVAL '1 12:43:21' DAY TO SECONDS) dt FROM test_emp GROUP BY dt ORDER BY 2 LIMIT 10;
660
+
661
+
count | dt
662
+
--------+-------------------------
663
+
10 | null
664
+
1 | 1952-02-28 12:00:00.000Z
665
+
1 | 1952-04-20 12:00:00.000Z
666
+
1 | 1952-05-16 12:00:00.000Z
667
+
1 | 1952-06-14 12:00:00.000Z
668
+
1 | 1952-07-09 12:00:00.000Z
669
+
1 | 1952-08-07 12:00:00.000Z
670
+
1 | 1952-11-14 12:00:00.000Z
671
+
1 | 1952-12-25 12:00:00.000Z
672
+
1 | 1953-01-08 12:00:00.000Z
673
+
;
674
+
614
675
dateTruncHaving
615
676
schema::gender:s|dt:ts
616
677
SELECT gender, max(hire_date) AS dt FROM test_emp GROUP BY gender HAVING DATE_TRUNC('year', max(hire_date)) >= '1997-01-01T00:00:00.000Z'::timestamp ORDER BY 1;
SELECT gender, max(hire_date) AS dt FROM test_emp GROUP BY gender HAVING max(hire_date) - DATE_TRUNC('hour', INTERVAL 1 YEARS) >= '1997-01-01T00:00:00.000Z'::timestamp ORDER BY 1;
689
+
690
+
gender | dt
691
+
--------+-------------------------
692
+
null | 1999-04-30 00:00:00.000Z
693
+
;
694
+
624
695
selectDatePartWithDate
625
696
SELECT DATE_PART('year', '2019-09-04'::date) as dp_years, DATE_PART('quarter', '2019-09-04'::date) as dp_quarter, DATE_PART('month', '2019-09-04'::date) as dp_month,
626
697
DATE_PART('dayofyear', '2019-09-04'::date) as dp_doy, DATE_PART('day', '2019-09-04'::date) as dp_day, DATE_PART('week', '2019-09-04'::date) as dp_week,
Copy file name to clipboardExpand all lines: x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeFunction.java
Copy file name to clipboardExpand all lines: x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePart.java
0 commit comments