SELECT
  '<img src="/main/image/pawn.png" style="width: 24px;">' AS PersonIcon,
  CC.ContactID,
  CC.Title,
  CC.FirstName,
  CC.Surname,
  TRIM(CONCAT(TRIM(COALESCE(CC.Title, '')), ' ', TRIM(CONCAT(TRIM(COALESCE(CC.FirstName, '')), ' ', TRIM(COALESCE(CC.Surname, '')))))) AS ContactName,
  CC.Position,
  CC.Email,
  REPLACE(REPLACE(REPLACE(IF(CONCAT(TRIM(COALESCE(CC.TelephoneAreaCode, '')), TRIM(COALESCE(CC.TelephoneNo, ''))) = '', '',
    TRIM(CONCAT('(', TRIM(COALESCE(CC.TelephoneAreaCode, '')), ') ', LEFT(COALESCE(CC.TelephoneNo, ''), 3), '-', MID(COALESCE(CC.TelephoneNo, ''), 4, 7)))
  ), '"', ''), '''', ''), '\\', '') AS ContactNumber,
  CASE CC.ContactType
    WHEN 'ClientBranch' THEN CONCAT('Client Branch : (', CC.TypeID, ') ', CB.BranchName)
    ELSE CC.ContactType
  END AS ContactType,
  CC.TelephoneAreaCode,
  CC.TelephoneNo,
  CC.Notify,
  '<img src=/main/image/RedCross.png>' AS DelIcon
FROM
  taClientContacts AS CC
    LEFT JOIN taClientBranches AS CB ON ((CB.ClientBranchID = CC.TypeID) AND (CC.ContactType = 'ClientBranch'))
WHERE
  1
  @pContactType
  @pTypeID
ORDER BY
  CC.Surname