-
Notifications
You must be signed in to change notification settings - Fork 1
/
timestamp_from_parts.sql
37 lines (35 loc) · 1.39 KB
/
timestamp_from_parts.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{%- macro timestamp_from_parts(date_field, time_field) -%}
{%- if target.type == 'snowflake' -%}
case
when len({{ date_field }}) > 0 and len({{ time_field }}) > 0
then timestamp_from_parts({{ date_field }}, try_to_time({{ time_field }}, '{{ var("time_format", "hh24:mi:ss.ff3") }}'))
else
timestamp_from_parts(null, null)
end
{%- elif target.type == 'sqlserver' -%}
case
when len({{ date_field }}) > 0 and len({{time_field}}) > 0
then datetime2fromparts(
datepart(year, {{ date_field }}),
datepart(month, {{ date_field }}),
datepart(day, {{ date_field }}),
datepart(hour, try_convert(time, {{ time_field }}, {{ var("time_format", 14) }})),
datepart(minute, try_convert(time, {{ time_field }}, {{ var("time_format", 14) }})),
datepart(second, try_convert(time, {{ time_field }}, {{ var("time_format", 14) }})),
datepart(millisecond, try_convert(time, {{ time_field }}, {{ var("time_format", 14) }})),
3
)
else
datetime2fromparts(
null,
null,
null,
null,
null,
null,
null,
3
)
end
{%- endif -%}
{%- endmacro -%}