select
    'DRIVER_CARD' as entity_type,
    cast(c.ID as varchar(128)) as source_entity_id,
    concat(coalesce(n.AlphaCode, ''), ':', c.CardNumber) as source_external_key,
    concat(coalesce(n.AlphaCode, ''), ':', c.CardNumber) as display_name,
    cast(case when c.IsLastCard = 1 and (c.ExpiryDate is null or c.ExpiryDate > getutcdate()) then 1 else 0 end as bit) as active,
    c.IssueDate as valid_from,
    c.ExpiryDate as valid_to,
    cast(null as datetime) as source_updated_at,
    c.ID as card_id,
    cast(c.ID_Driver as varchar(128)) as driver_id,
    n.AlphaCode as card_nation,
    c.CardNumber as card_number,
    c.Consecutive as consecutive,
    c.Replacement as replacement,
    c.Renewal as renewal,
    c.IssueDate as issue_date,
    c.ExpiryDate as expiry_date,
    c.ValidityDate as validity_date,
    c.Authorityname as authority_name,
    c.ID_FileLog as file_log_id,
    c.IsAnalog as is_analog,
    c.IsLastCard as is_last_card,
    c.Generation as generation
from dbo.Card c
left join dbo.Nation n on n.ID = c.ID_Nation
where c.CardNumber is not null
