|
1 | 1 | --- |
2 | | -title: DateTimeBin in Azure Cosmos DB query language |
3 | | -description: Learn about SQL system function DateTimeBin in Azure Cosmos DB. |
4 | | -author: jcocchi |
5 | | -ms.service: cosmos-db |
6 | | -ms.subservice: nosql |
7 | | -ms.topic: conceptual |
8 | | -ms.date: 05/27/2022 |
9 | | -ms.author: jucocchi |
10 | | -ms.custom: query-reference, ignite-2022 |
11 | | ---- |
12 | | - |
13 | | -# DateTimeBin (Azure Cosmos DB) |
14 | | - [!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)] |
15 | | - |
16 | | -Returns the nearest multiple of *BinSize* below the specified DateTime given the unit of measurement *DateTimePart* and start value of *BinAtDateTime*. |
17 | | - |
18 | | - |
19 | | -## Syntax |
20 | | - |
21 | | -```sql |
22 | | -DateTimeBin (<DateTime> , <DateTimePart> [,BinSize] [,BinAtDateTime]) |
23 | | -``` |
24 | | - |
25 | | - |
26 | | -## Arguments |
27 | | - |
28 | | -*DateTime* |
29 | | - The string value date and time to be binned. A UTC date and time ISO 8601 string value in the format `YYYY-MM-DDThh:mm:ss.fffffffZ` where: |
30 | | - |
31 | | -|Format|Description| |
32 | | -|-|-| |
33 | | -|YYYY|four-digit year| |
34 | | -|MM|two-digit month (01 = January, etc.)| |
35 | | -|DD|two-digit day of month (01 through 31)| |
36 | | -|T|signifier for beginning of time elements| |
37 | | -|hh|two-digit hour (00 through 23)| |
38 | | -|mm|two-digit minutes (00 through 59)| |
39 | | -|ss|two-digit seconds (00 through 59)| |
40 | | -|.fffffff|seven-digit fractional seconds| |
41 | | -|Z|UTC (Coordinated Universal Time) designator| |
42 | | - |
43 | | -For more information on the ISO 8601 format, see [ISO_8601](https://en.wikipedia.org/wiki/ISO_8601) |
44 | | - |
45 | | -*DateTimePart* |
46 | | - The date time part specifies the units for BinSize. DateTimeBin is Undefined for DayOfWeek, Year, and Month. The finest granularity for binning by Nanosecond is 100 nanosecond ticks; if Nanosecond is specified with a BinSize less than 100, the result is Undefined. This table lists all valid DateTimePart arguments for DateTimeBin: |
47 | | - |
48 | | -| DateTimePart | abbreviations | |
49 | | -| ------------ | -------------------- | |
50 | | -| Day | "day", "dd", "d" | |
51 | | -| Hour | "hour", "hh" | |
52 | | -| Minute | "minute", "mi", "n" | |
53 | | -| Second | "second", "ss", "s" | |
54 | | -| Millisecond | "millisecond", "ms" | |
55 | | -| Microsecond | "microsecond", "mcs" | |
56 | | -| Nanosecond | "nanosecond", "ns" | |
57 | | - |
58 | | -*BinSize* (optional) |
59 | | - Numeric value that specifies the size of bins. If not specified, the default value is one. |
| 2 | +title: DateTimeBin |
| 3 | +titleSuffix: Azure Cosmos DB for NoSQL |
| 4 | +description: An Azure Cosmos DB for NoSQL system function that returns a date and time that's the resulting of binning (rounding) a part of the specified datetime. |
| 5 | +author: jcodella |
| 6 | +ms.author: jacodel |
| 7 | +ms.reviewer: sidandrews |
| 8 | +ms.service: cosmos-db |
| 9 | +ms.subservice: nosql |
| 10 | +ms.topic: reference |
| 11 | +ms.date: 07/19/2023 |
| 12 | +ms.custom: query-reference |
| 13 | +--- |
60 | 14 |
|
| 15 | +# DateTimeBin (NoSQL query) |
61 | 16 |
|
62 | | -*BinAtDateTime* (optional) |
63 | | - A UTC date and time ISO 8601 string value in the format `YYYY-MM-DDThh:mm:ss.fffffffZ` that specifies the start date to bin from. Default value is the Unix epoch, ‘1970-01-01T00:00:00.000000Z’. |
| 17 | +[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)] |
64 | 18 |
|
| 19 | +Returns a date and time string value that is the result of binning (or rounding) a part of the provided date and time string. |
65 | 20 |
|
66 | | -## Return types |
| 21 | +## Syntax |
67 | 22 |
|
68 | | -Returns the result of binning the *DateTime* value. |
| 23 | +```sql |
| 24 | +DateTimeBin(<date_time> , <date_time_part> [, <bin_size>] [, <bin_start_date_time>]) |
| 25 | +``` |
69 | 26 |
|
| 27 | +## Arguments |
70 | 28 |
|
71 | | -## Remarks |
| 29 | +| | Description | |
| 30 | +| --- | --- | |
| 31 | +| **`date_time`** | A Coordinated Universal Time (UTC) date and time string in the ISO 8601 format `YYYY-MM-DDThh:mm:ss.fffffffZ`. | |
| 32 | +| **`date_time_part`** | A string representing a part of an ISO 8601 date format specification. This part is used to indicate which aspect of the date to bin. Specifically, this part argument represents the level of granularity for binning (or rounding). The minimum granularity for the part is **days** and the maximum granularity is **nanoseconds**. | |
| 33 | +| **`bin_size` *(Optional)*** | An optional numeric value specifying the size of the bin. If not specified, the default value is `1`. | |
| 34 | +| **`bin_start_date_time` *(Optional)*** | An optional Coordinated Universal Time (UTC) date and time string in the ISO 8601 format `YYYY-MM-DDThh:mm:ss.fffffffZ`. This date and time argument specifies the start date to bin from. If not specified, the default value is the Unix epoch `1970-01-01T00:00:00.000000Z`. | |
72 | 35 |
|
73 | | -DateTimeBin will return `Undefined` for the following reasons: |
74 | | -- The DateTimePart value specified is invalid |
75 | | -- The BinSize value is zero or negative |
76 | | -- The DateTime or BinAtDateTime isn't a valid ISO 8601 DateTime or precedes the year 1601 (the Windows epoch) |
| 36 | +> [!NOTE] |
| 37 | +> For more information on the ISO 8601 format, see [ISO 8601](https://wikipedia.org/wiki/ISO_8601). For more information on the Unix epoch, see [Unix time](https://wikipedia.org/wiki/unix_time). |
77 | 38 |
|
| 39 | +## Return types |
78 | 40 |
|
79 | | -## Examples |
| 41 | +Returns a UTC date and time string in the ISO 8601 format `YYYY-MM-DDThh:mm:ss.fffffffZ`. |
80 | 42 |
|
81 | | -The following example bins ‘2021-06-28T17:24:29.2991234Z’ by one hour: |
| 43 | +## Examples |
82 | 44 |
|
83 | | -```sql |
84 | | -SELECT DateTimeBin('2021-06-28T17:24:29.2991234Z', 'hh') AS BinByHour |
85 | | -``` |
| 45 | +The following example bins the date **January 8, 2021** at **18:35 UTC** by various values. The example also changes the bin size, and the bin start date and time. |
86 | 46 |
|
87 | | -```json |
88 | | -[ |
89 | | - { |
90 | | - "BinByHour": "2021-06-28T17:00:00.0000000Z" |
91 | | - } |
92 | | -] |
93 | | -``` |
| 47 | +:::code language="sql" source="~/cosmos-db-nosql-query-samples/scripts/datetimebin/query.sql" highlight="2-7"::: |
94 | 48 |
|
95 | | -The following example bins ‘2021-06-28T17:24:29.2991234Z’ given different *BinAtDateTime* values: |
| 49 | +:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/datetimebin/result.json"::: |
96 | 50 |
|
97 | | -```sql |
98 | | -SELECT |
99 | | -DateTimeBin('2021-06-28T17:24:29.2991234Z', 'day', 5) AS One_BinByFiveDaysUnixEpochImplicit, |
100 | | -DateTimeBin('2021-06-28T17:24:29.2991234Z', 'day', 5, '1970-01-01T00:00:00.0000000Z') AS Two_BinByFiveDaysUnixEpochExplicit, |
101 | | -DateTimeBin('2021-06-28T17:24:29.2991234Z', 'day', 5, '1601-01-01T00:00:00.0000000Z') AS Three_BinByFiveDaysFromWindowsEpoch, |
102 | | -DateTimeBin('2021-06-28T17:24:29.2991234Z', 'day', 5, '2021-01-01T00:00:00.0000000Z') AS Four_BinByFiveDaysFromYearStart, |
103 | | -DateTimeBin('2021-06-28T17:24:29.2991234Z', 'day', 5, '0001-01-01T00:00:00.0000000Z') AS Five_BinByFiveDaysFromUndefinedYear |
104 | | -``` |
| 51 | +## Remarks |
105 | 52 |
|
106 | | -```json |
107 | | -[ |
108 | | - { |
109 | | - "One_BinByFiveDaysUnixEpochImplicit": "2021-06-27T00:00:00.0000000Z", |
110 | | - "Two_BinByFiveDaysUnixEpochExplicit": "2021-06-27T00:00:00.0000000Z", |
111 | | - "Three_BinByFiveDaysFromWindowsEpoch": "2021-06-28T00:00:00.0000000Z", |
112 | | - "Four_BinByFiveDaysFromYearStart": "2021-06-25T00:00:00.0000000Z" |
113 | | - } |
114 | | -] |
115 | | -``` |
| 53 | +- This function returns `undefined` for these reasons: |
| 54 | + - The specified date and time part is invalid. |
| 55 | + - The bin size value isn't a valid integer, is zero, or is negative. |
| 56 | + - The date and time in either argument isn't a valid ISO 8601 date and time string. |
| 57 | + - The date and time for the bin start precedes the year `1601`, the Windows epoch. |
| 58 | +- The ISO 8601 date format specifies valid date and time parts to use with this function: |
| 59 | + | | Format | |
| 60 | + | --- | --- | |
| 61 | + | **Day** | `day`, `dd`, `d` | |
| 62 | + | **Hour** | `hour`, `hh` | |
| 63 | + | **Minute** | `minute`, `mi`, `n` | |
| 64 | + | **Second** | `second`, `ss`, `s` | |
| 65 | + | **Millisecond** | `millisecond`, `ms` | |
| 66 | + | **Microsecond** | `microsecond`, `mcs` | |
| 67 | + | **Nanosecond** | `nanosecond`, `ns` | |
116 | 68 |
|
117 | | -## Next steps |
| 69 | +## Next steps |
118 | 70 |
|
119 | | -- [System functions Azure Cosmos DB](system-functions.yml) |
120 | | -- [Introduction to Azure Cosmos DB](../../introduction.md) |
| 71 | +- [System functions Azure Cosmos DB](system-functions.yml) |
| 72 | +- [`DateTimeAdd`](datetimeadd.md) |
0 commit comments