select
    'DRIVER' as entity_type,
    cast(d.ID as varchar(128)) as source_entity_id,
    coalesce(nullif(d.IdentificationNumber, ''), nullif(d.LicenseNumber, ''), cast(d.ID as varchar(128))) as source_external_key,
    ltrim(rtrim(concat(d.Surname, ' ', d.Firstnames))) as display_name,
    cast(case when d.IsActive = 1 then 1 else 0 end as bit) as active,
    d.EmploymentStartDate as valid_from,
    d.RetirementDate as valid_to,
    d.LastUpdate as source_updated_at,
    d.ID as driver_id,
    d.Surname as surname,
    d.Surname as last_name,
    d.Firstnames as first_names,
    d.Birthdate as birth_date,
    d.BirthPlace as birth_place,
    d.LicenseNumber as license_number,
    ln.AlphaCode as license_nation,
    d.LicenseAuthority as license_authority,
    d.LExpiryDate as license_expiry_date,
    d.Phone as phone,
    d.Mobile as mobile,
    d.Email as email,
    d.CostCenter as cost_center,
    d.OrganisationUserID as organisation_user_id,
    d.IdentificationNumber as identification_number,
    d.Region as region,
    d.ID_FileLog as file_log_id,
    d.LastFileLog_ID as last_file_log_id,
    d.FirstDrivingTime as first_driving_time,
    d.LastDrivingTime as last_driving_time,
    d.LastUsedVehicle_ID as last_used_vehicle_id,
    d.LastUsedVehicleTime as last_used_vehicle_time,
    d.IsDigital as is_digital,
    d.IsAnalog as is_analog
from dbo.Driver d
left join dbo.Nation ln on ln.ID = d.ID_LicenseNation
