tsql-utils icon indicating copy to clipboard operation
tsql-utils copied to clipboard

`sqlserver__dateadd()` does not work on Synapse

Open dataders opened this issue 4 years ago • 0 comments

for some reason sqlserver__dateadd works on Azure SQL, but on Synapse, it comes up short by a day.

actual expected
1900-01-02 00 00 00.0000000 1900-01-01 00 00 00.000

https://github.com/dbt-msft/tsql-utils/blob/730fa0d0ce2a4d3bdca3bf94d09f7065e5e5370c/macros/dbt_utils/cross_db_utils/dateadd.sql#L1-L13

with data as (

    select * from "dbtsynapseci"."test"."data_dateadd"

)

select
    case
        when datepart = 'hour' then cast(

    dateadd(
        hour,
        interval_length,
        cast(from_time as datetime)
        )

 as 
    
    
    datetime2
)
        when datepart = 'day' then cast(

    dateadd(
        day,
        interval_length,
        cast(from_time as datetime)
        )

 as 
    
    
    datetime2
)
        when datepart = 'month' then cast(

    dateadd(
        month,
        interval_length,
        cast(from_time as datetime)
        )

 as 
    
    
    datetime2
)
        when datepart = 'year' then cast(

    dateadd(
        year,
        interval_length,
        cast(from_time as datetime)
        )

 as 
    
    
    datetime2
)
        else null
    end as actual,
    result as expected

from data

dataders avatar Nov 09 '21 18:11 dataders