DECLARE @vers VARCHAR(20);
SELECT @vers=CAST(SERVERPROPERTY('productversion') AS VARCHAR(20));
IF @vers LIKE '16.%'
BEGIN
    SELECT COUNT(1) AS db_count
        , compatibility_level 
        , MAX(STUFF(CONVERT(VARCHAR(MAX), x.[db_Lst]),1,1,'')) AS lst
    FROM sys.databases d
        OUTER APPLY (
            SELECT ','+name
            FROM sys.databases dd
            WHERE dd.compatibility_level = d.compatibility_level 
            FOR XML PATH('')
        )x(db_Lst)
    WHERE d.compatibility_level <> 160 -- 160=SQL2022
    GROUP BY compatibility_level
END
