Skip to content

Commit f52576e

Browse files
committed
Document DATETRUNC
1 parent c56a5e9 commit f52576e

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

content/en/docs/refguide/modeling/domain-model/oql/oql-expression-syntax.md

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ These are the currently supported functions:
687687
* COALESCE
688688
* DATEDIFF
689689
* DATEPART
690+
* DATETRUNC
690691
* LENGTH
691692
* LOWER
692693
* RANGEBEGIN
@@ -875,7 +876,7 @@ DATEDIFF ( unit , startdate_expression, enddate_expression [, timezone ] )
875876
* `MINUTE`,
876877
* `SECOND`
877878
* `MILLISECOND`.
878-
879+
879880
For more information on `DATETIME` values, see the [example section under *DATEPART*](#oql-datepart-example), below.
880881

881882
##### startdate_expression
@@ -888,7 +889,11 @@ For more information on `DATETIME` values, see the [example section under *DATEP
888889

889890
##### timezone
890891

891-
`timezone` specifies the time zone to use for the retrieval. This parameter is optional and defaults to the local time zone. It should be a string literal containing an [IANA time zone](https://www.iana.org/time-zones). GMT offset time zones are not supported.
892+
`timezone` specifies the time zone to use for the retrieval. This parameter is optional and defaults to the user time zone. It should be a string literal containing an [IANA time zone](https://www.iana.org/time-zones). GMT offset time zones are not supported.
893+
894+
{{% alert color="info" %}}
895+
The user time zone is usually different from UTC. To get the result in the UTC time zone, explicitly specify `'UTC'` in this parameter. For details on time zone handling in Mendix Runtime, see [Date and Time Handling](/refguide/date-and-time-handling/).
896+
{{% /alert %}}
892897

893898
#### Examples
894899

@@ -953,7 +958,11 @@ DATEPART ( datepart , date_expression [, timezone ] )
953958

954959
##### timezone
955960

956-
`timezone` specifies the time zone to use for the retrieval. This parameter is optional and defaults to the local time zone. It should be a string literal containing an IANA time zone. GMT offset time zones are not supported.
961+
`timezone` specifies the time zone to use for the retrieval. This parameter is optional and defaults to the user time zone. It should be a string literal containing an IANA time zone. GMT offset time zones are not supported.
962+
963+
{{% alert color="info" %}}
964+
The user time zone is usually different from UTC. To get the result in the UTC time zone, explicitly specify `'UTC'` in this parameter. For details on time zone handling in Mendix Runtime, see [Date and Time Handling](/refguide/date-and-time-handling/).
965+
{{% /alert %}}
957966

958967
#### Examples{#oql-datepart-example}
959968

@@ -981,6 +990,74 @@ SELECT End FROM Sales.Period WHERE DATEPART(YEAR, End) = 2025
981990
|---------------------|
982991
| 2025-07-05 00:00:00 |
983992

993+
### DATETRUNC {#datetrunc-function}
994+
995+
The `DATETRUNC` function truncates a `DATETIME` value to a specified datepart. The return type is `DATETIME`.
996+
997+
This function was introduced in Mendix version 11.9.0
998+
999+
#### Syntax
1000+
1001+
The syntax is as follows:
1002+
1003+
```sql
1004+
DATETRUNC ( datepart , date_expression [, timezone ] )
1005+
```
1006+
1007+
##### datepart
1008+
1009+
`datepart` specifies the part to which the `DATETIME` value is truncated. For possible values, see the [Example](#oql-datetrunc-example) below.
1010+
1011+
##### date_expression
1012+
1013+
`date_expression` specifies the date to retrieve an element from. The expression should resolve to a `DATETIME` value. String representations of `DATETIME` are accepted.
1014+
1015+
##### timezone
1016+
1017+
`timezone` specifies the time zone to use for truncation. This parameter is optional and defaults to the user time zone. It should be a string literal containing an IANA time zone. GMT offset time zones are not supported.
1018+
1019+
{{% alert color="info" %}}
1020+
The user time zone is usually different from UTC. To get the result in the UTC time zone, explicitly specify `'UTC'` in this parameter. For details on time zone handling in Mendix Runtime, see [Date and Time Handling](/refguide/date-and-time-handling/).
1021+
{{% /alert %}}
1022+
1023+
#### Examples{#oql-datetrunc-example}
1024+
1025+
| datepart | Truncation result for `2005-09-03T16:34:20.356` |
1026+
|--------------|-------------------------------------------------|
1027+
| `YEAR` | `2005-01-01T00:00:00.000` |
1028+
| `QUARTER` | `2005-07-01T00:00:00.000` |
1029+
| `MONTH` | `2005-09-01T00:00:00.000` |
1030+
| `DAY` | `2005-09-03T00:00:00.000` |
1031+
| `WEEK`* | `2005-07-29T00:00:00.000` |
1032+
| `HOUR` | `2005-09-03T16:00:00.000` |
1033+
| `MINUTE` | `2005-09-03T16:34:00.000` |
1034+
| `SECOND` | `2005-09-03T16:34:20.000` |
1035+
1036+
{{% alert color="info" %}}
1037+
Date part types `DAYOFYEAR`, `WEEKDAY` and `MILLISECOND` are not supported by the `DATETRUNC` function
1038+
{{% /alert %}}
1039+
1040+
{{% alert color="info" %}}
1041+
For the date part type `WEEK`, the result of the `DATETRUNC` function depends on the database configuration. For example, by default, the first day of the week in MS SQL Server is Sunday, which means that dates are truncated to previous Sunday if date part type `WEEK` is used.
1042+
{{% /alert %}}
1043+
1044+
`DATETRUNC` function can be used to group data by time periods:
1045+
1046+
```sql
1047+
SELECT
1048+
DATETRUNC(QUARTER, End) AS PeriodEndQuarter,
1049+
SUM(Revenue) AS QuarterPeriodRevenue
1050+
FROM
1051+
Sales.Period
1052+
GROUP BY
1053+
DATETRUNC(QUARTER, End)
1054+
```
1055+
1056+
| PeriodEndQuarter | QuarterPeriodRevenue |
1057+
|---------------------|----------------------|
1058+
| 2024-04-01 00:00:00 | 10 |
1059+
| 2025-07-01 00:00:00 | 28 |
1060+
9841061
### LENGTH {#length-function}
9851062

9861063
#### Description

0 commit comments

Comments
 (0)