azure sql database – Error converting data type nvarchar to bigint

a failing piece of code extracted from the procedure:

    declare @row UNIQUEIDENTIFIER
    declare @count BIGINT
    declare @deleteToDate BIGINT
    declare @deleteToDateYesterday BIGINT
    declare @DaysMiliseconds BIGINT
    declare @MilisecondsYesterday BIGINT
    declare @Time DATETIME
    
    SET @Time = SYSDATETIME()
    SET @count = 0
    -- The number 365 is included here because it is a procedure parameter - you can ignore it. If I don't use CAST, I get "Arithmetic overflow error converting expression to data type int."
    SET @DaysMiliseconds = CAST(365 AS BIGINT) * 24 * 60 * 60 * 1000
    SET @MilisecondsYesterday = 1.3 * 24 * 60 * 60 * 1000

    SET @deleteToDate = (SELECT DATEDIFF_BIG(MILLISECOND,'1970-01-01 00:00:00.000', SYSDATETIME()) - @DaysMiliseconds Milliseconds)
    SET @deleteToDateYesterday = (SELECT DATEDIFF_BIG(MILLISECOND,'1970-01-01 00:00:00.000', SYSDATETIME()) - @MilisecondsYesterday Milliseconds)

    select id from [x-dev].master where (validUntil < @deleteToDateYesterday OR validUntil IS NULL) AND updatedAt < @deleteToDate;

The result is the following error

Msg 8114, Level 16, State 5, Line 18
Error converting data type nvarchar to bigint.

SQL version:
Microsoft SQL Azure (RTM) – 12.0.2000.8 Nov 2 2023 01:40:17 Copyright (C) 2022 Microsoft Corporation

I would be very grateful for any help.

Read more here: Source link