|
| 1 | +--- |
| 2 | +Title: Formatting date and time values |
| 3 | +alwaysopen: false |
| 4 | +categories: |
| 5 | +- docs |
| 6 | +- integrate |
| 7 | +- rs |
| 8 | +- rdi |
| 9 | +description: null |
| 10 | +group: di |
| 11 | +linkTitle: Formatting date and time values |
| 12 | +summary: Redis Data Integration keeps Redis in sync with a primary database in near |
| 13 | + real time. |
| 14 | +type: integration |
| 15 | +weight: 40 |
| 16 | +--- |
| 17 | + |
| 18 | +The way you format date and time values depends on the source database, the data type of the field, and how it is represented in the incoming record. Below are some examples for different databases and data types. |
| 19 | + |
| 20 | +## Oracle |
| 21 | + |
| 22 | +Oracle supports the following date and time data types: |
| 23 | + |
| 24 | +- `DATE` - represented by Debezium as a 64-bit integer representing the milliseconds since epoch |
| 25 | + ```yaml |
| 26 | + transform: |
| 27 | + - uses: add_field |
| 28 | + with: |
| 29 | + fields: |
| 30 | + - field: formatted_date |
| 31 | + language: sql |
| 32 | + # Date is stored as a Unix timestamp in milliseconds so you need to |
| 33 | + # divide it by 1000 to convert it to seconds. |
| 34 | + expression: STRFTIME('%Y-%m-%d %H:%M:%S', DATE / 1000, 'unixepoch') |
| 35 | + # Example: 1749047572000 is transformed to 2025-06-04 14:32:52 |
| 36 | + ``` |
| 37 | +- `TIMESTAMP` - the value is represented by Debezium as a 64-bit integer and depends on the number of decimal places of precision of the column, representing fractions of a second. For example, if the column is defined as `TIMESTAMP(6)`, there are six decimal places and so the value is represented as microseconds since epoch (since there are 10^6 microseconds in each second). |
| 38 | +You can format it similarly to `DATE`, but you need to divide the value by the appropriate factor based on the precision. |
| 39 | + |
| 40 | +- `TIMESTAMP WITH TIME ZONE` - the value is represented as a string containing the timestamp and time zone. |
| 41 | + |
| 42 | +- `TIMESTAMP WITH LOCAL TIME ZONE` - the value is represented as a string containing the timestamp and local time zone. |
| 43 | + |
| 44 | + SQLite supports both `TIMESTAMP WITH TIME ZONE` and `TIMESTAMP WITH LOCAL TIME ZONE`. You can format them using the `STRFTIME` function. |
| 45 | + |
| 46 | + ```yaml |
| 47 | + transform: |
| 48 | + - uses: add_field |
| 49 | + with: |
| 50 | + fields: |
| 51 | + - field: seconds_since_epoch |
| 52 | + language: sql |
| 53 | + # Convert the timestamp with local time zone to seconds since epoch. |
| 54 | + expression: STRFTIME('%s', TIMESTAMP_FIELD) |
| 55 | + |
| 56 | + - field: date_from_timestamp |
| 57 | + language: sql |
| 58 | + # Convert the timestamp with local time zone to date and time. |
| 59 | + expression: STRFTIME('%Y-%m-%d %H:%M:%S', TIMESTAMP_FIELD) |
| 60 | + ``` |
| 61 | +
|
| 62 | +---- |
| 63 | +
|
| 64 | +## SQL Server |
| 65 | +SQL Server supports the following date and time data types: |
| 66 | +
|
| 67 | +- `date` - represented by Debezium as number of days since epoch (1970-01-01). You can multiply the value by 86400 (the number of seconds in a day) to convert it to seconds since epoch and then use the `STRFTIME` or `DATE` functions to format it. |
| 68 | + ```yaml |
| 69 | + transform: |
| 70 | + - uses: add_field |
| 71 | + with: |
| 72 | + fields: |
| 73 | + - field: with_default_date_format |
| 74 | + language: sql |
| 75 | + # Uses the default DATE format |
| 76 | + expression: DATE(event_date * 86400, 'unixepoch') |
| 77 | + |
| 78 | + - field: with_custom_date_format |
| 79 | + language: sql |
| 80 | + # Uses the default DATE format |
| 81 | + expression: STRFTIME('%Y/%m/%d', event_date * 86400, 'unixepoch') |
| 82 | + ``` |
| 83 | + |
| 84 | +- `datetime`, `smalldatetime` - represented by Debezium as number of milliseconds since epoch. Divide the value by 1000 to convert it to seconds since epoch and then use the `STRFTIME` function to format it. |
| 85 | + ```yaml |
| 86 | + transform: |
| 87 | + - uses: add_field |
| 88 | + with: |
| 89 | + fields: |
| 90 | + - field: formatted_datetime |
| 91 | + language: sql |
| 92 | + expression: STRFTIME('%Y-%m-%d %H:%M:%S', event_datetime / 1000, 'unixepoch') |
| 93 | + ``` |
| 94 | + |
| 95 | +- `datetime2` - similar to `datetime` but with higher precision. For `datetime2(0-3)`, the representation is the same as for `datetime`. For `datetime2(4-6)`, it is the number of microseconds since epoch. For `datetime2(7)`, it is the number of nanoseconds since epoch. To convert to another time unit, you can use the same approach as for `datetime` but you need to divide by 1000, 1000000 or 1000000000 depending on the precision. |
| 96 | + |
| 97 | +- `time` - the number of milliseconds since midnight. |
| 98 | + ```yaml |
| 99 | + transform: |
| 100 | + - uses: add_field |
| 101 | + with: |
| 102 | + fields: |
| 103 | + - field: formatted_time |
| 104 | + language: sql |
| 105 | + expression: TIME(event_time, 'unixepoch', 'utc') |
| 106 | + ``` |
| 107 | + |
| 108 | +- `datetimeoffset` - represented as a timestamp with timezone information (for example, `2025-05-27T15:21:42.864Z` or `2025-01-02T14:45:30.123+05:00`). |
| 109 | + ```yaml |
| 110 | + transform: |
| 111 | + - uses: add_field |
| 112 | + with: |
| 113 | + fields: |
| 114 | + - field: formatted_datetimeoffset |
| 115 | + language: sql |
| 116 | + expression: STRFTIME('%Y-%m-%d %H:%M:%S', event_datetimeoffset) |
| 117 | + ``` |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +<!-- TODO [ilianiliev-redis]: Test and document the dynamic expressions for the rest of the supported databases - MySQL, PostgresSQL, MongoDB --> |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +---- |
| 127 | + |
| 128 | +## PostgreSQL |
| 129 | + |
| 130 | +PostgreSQL supports the following date and time data types: |
| 131 | + |
| 132 | +- `date` - represented by Debezium as number of days since epoch (1970-01-01). You can multiply the value by 86400 (the number of seconds in a day) to convert it to seconds since epoch and then use the `STRFTIME` or `DATE` functions to format it. |
| 133 | + ```yaml |
| 134 | + transform: |
| 135 | + - uses: add_field |
| 136 | + with: |
| 137 | + fields: |
| 138 | + - field: with_default_date_format |
| 139 | + language: sql |
| 140 | + # Uses the default DATE format |
| 141 | + expression: DATE(event_date * 86400, 'unixepoch') |
| 142 | + ``` |
| 143 | + |
| 144 | +- `time` - the time of microseconds since midnight. |
| 145 | + ```yaml |
| 146 | + transform: |
| 147 | + - uses: add_field |
| 148 | + with: |
| 149 | + fields: |
| 150 | + - field: formatted_time |
| 151 | + language: sql |
| 152 | + # Divide by 1000000 to convert microseconds to seconds |
| 153 | + expression: TIME(event_time / 1000000, 'unixepoch', 'utc') |
| 154 | + ``` |
| 155 | + |
| 156 | +- `time with time zone` - a string representation of the time with timezone information, where the timezone is GMT (for example, `07:15:00Z`). |
| 157 | + ```yaml |
| 158 | + transform: |
| 159 | + - uses: add_field |
| 160 | + with: |
| 161 | + fields: |
| 162 | + - field: formatted_time_with_tz |
| 163 | + language: sql |
| 164 | + expression: STRFTIME('%H:%M:%S', event_time_with_time_zone) |
| 165 | + ``` |
| 166 | + |
| 167 | +- `timestamp` - represented by Debezium as a 64-bit integer containing the microseconds since epoch. You can use the `STRFTIME` function to format it. |
| 168 | + ```yaml |
| 169 | + transform: |
| 170 | + - uses: add_field |
| 171 | + with: |
| 172 | + fields: |
| 173 | + - field: formatted_timestamp |
| 174 | + language: sql |
| 175 | + # Divide by 1000000 to convert microseconds to seconds |
| 176 | + expression: STRFTIME('%Y-%m-%d %H:%M:%S', event_timestamp / 1000000, 'unixepoch') |
| 177 | + ``` |
| 178 | + |
| 179 | +- `timestamp with time zone` - represented by Debezium as a string containing the timestamp with time zone information, where the timezone is GMT (for example, `2025-06-07T10:15:00.000000Z`). |
| 180 | + ```yaml |
| 181 | + transform: |
| 182 | + - uses: add_field |
| 183 | + with: |
| 184 | + fields: |
| 185 | + - field: formatted_timestamp_with_tz |
| 186 | + language: sql |
| 187 | + # Divide by 1000000 to convert microseconds to seconds |
| 188 | + expression: STRFTIME('%Y-%m-%d %H:%M:%S', event_timestamp_with_time_zone) |
| 189 | + ``` |
0 commit comments