Skip to content

Commit f58e252

Browse files
Adding examples for SQL Server
1 parent 5f19d9e commit f58e252

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

content/integrate/redis-data-integration/data-pipelines/transform-examples/redis-expiration-example.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ output:
4949
language: jmespath
5050
```
5151

52-
Settings the expiration according to a field that contains a date, datetime, or timestamp value is also possible, but it depends on the source database and the data types it supports.
52+
## Dynamic expiration time based on a date, datetime, or timestamp field
5353

54+
Settings the expiration according to a field that contains a date, datetime, or timestamp value is also possible, but it depends on the source database and the data types it supports. Please refer to the examples below for your specific source database and data type.
55+
56+
{{< expand "Oracle examples" >}}
5457

55-
### Oracle examples
5658
The transformation depends on the data type of the field in the source database:
5759

5860
- `DATE` - represented by debezium as 64-bit integer representing the milliseconds since epoch
@@ -100,3 +102,62 @@ The transformation depends on the data type of the field in the source database:
100102
expression: CASE WHEN expire_seconds < 0 THEN -expire_seconds ELSE -1 END
101103
language: sql
102104
```
105+
{{< /expand >}}
106+
107+
108+
{{< expand "SQL Server examples" >}}
109+
SQL Server supports the following date and time data types:
110+
111+
- `date` - represented in Debezium as number of days since epoch (1970-01-01). Please note that due to the lack of time information, this method is not very accurate.
112+
```yaml
113+
output:
114+
- uses: redis.write
115+
with:
116+
data_type: hash
117+
expire:
118+
# We calculate the number of seconds for the amount of days and subtract the current time in seconds since epoch.
119+
expression: (event_date * 86400) - strftime('%s', 'now')
120+
language: sql
121+
```
122+
123+
- `datetime`, `smalldatetime` - represented in Debezium as number of milliseconds since epoch.
124+
```yaml
125+
output:
126+
- uses: redis.write
127+
with:
128+
data_type: hash
129+
expire:
130+
# Due to event_datetime being in milisecond we need to divide it by 1000 to convert it to seconds.
131+
expression: event_datetime / 1000 - strftime('%s', 'now')
132+
language: sql
133+
```
134+
- `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. and for `datetime2(7)` it is the number of nanoseconds since epoch. You can use the same approach as for `datetime` but you need to divide by 1000, 1000000 or 1000000000 depending on the precision.
135+
136+
- `time` - the time of milliseconds since midnight.
137+
```yaml
138+
output:
139+
- uses: redis.write
140+
with:
141+
data_type: hash
142+
expire:
143+
# We convert the time to seconds and subtract the current time in seconds since midnight.
144+
expression: (event_time / 1000.0) -
145+
(
146+
CAST(strftime('%H', 'now') AS INTEGER) * 3600 +
147+
CAST(strftime('%M', 'now') AS INTEGER) * 60 +
148+
CAST(strftime('%S', 'now') AS INTEGER)
149+
)
150+
language: sql
151+
```
152+
- `datetimeoffset` - represented as a timestamp with timezone information, where the timezone is GMT
153+
```yaml
154+
output:
155+
- uses: redis.write
156+
with:
157+
data_type: hash
158+
expire:
159+
# We convert the time to seconds and subtract the current time in seconds since epoch.
160+
expression: strftime('%s', event_datetimeoffset) - strftime('%s', 'now')
161+
language: sql
162+
```
163+
{{< /expand >}}

0 commit comments

Comments
 (0)