Constraint on existing table - unique combination of values only when another column is null
Blatantly copied from A-K's answer in the similar question (Can I add a unique constraint that ignores existing violations?) and adjusted for this one.

It assumes there is an ID column with a unique constraint:

ALTER TABLE dbo.Example
ADD IgnoreThisDuplicate integer NULL;
GO
UPDATE E
SET IgnoreThisDuplicate = ID
FROM dbo.Example AS E
WHERE EXISTS 
( 
    SELECT *
    FROM dbo.Example AS P1
    WHERE 
        P1.Username = E.Username
        AND P1.Gateway = E.Gateway
        AND P1.Code = E.Code
        AND P1.ActiveTo IS NULL 
        AND E.ActiveTo IS NULL
        AND P1.ID &lt; E.ID
);
GO
-- This constraint is not trusted
ALTER TABLE dbo.Example 
WITH NOCHECK
    ADD CONSTRAINT CHK_Example_NoNewDuplicates 
    CHECK (IgnoreThisDuplicate IS NULL);
GO
-- Applying the constraint to the table above would ignore the two user1 duplicates.
GO
SELECT * FROM dbo.Example;
GO
CREATE UNIQUE INDEX 
    UNQ_Example_UniqueNewUsernameGatewayCode 
ON dbo.Example
    (Username, Gateway, Code, IgnoreThisDuplicate)
WHERE 
    ActiveTo IS NULL;


Test it out: db&lt;&gt;fiddle demo