SELECT DeptCode1 + DeptCode2 + DeptCode3 AS dpcode,
CASE WHEN deptname2 = '' THEN deptname1 WHEN deptname2 <> '' AND
deptname3 = '' THEN DeptName1 + '\' + DeptName2 WHEN deptname2 <> '' AND
deptname3 <> '' THEN DeptName1 + '\' + DeptName2 + '\' + DeptName3 END AS dpname
FROM dbo.Department