select rl.Linenumber,
'0x' + Substring(c.CardString, 2,5) AS Hex
into #T1
from ReservationCardXTab rc (nolock)
inner join Card c (nolock) on rc.FK_Card = c.PK_Card
inner join CardTypeXTab ctx on c.PK_Card = ctx.FK_Card and ctx.FK_CardType = 5
inner join reservations r (nolock) on rc.ReferenceNumber = r.ReferenceNumber and rc.GuestNumber = r.GuestNumber
inner join RESERVATIONROOMLINEXTAB rlx (nolock) on rlx.ReferenceNumber = r.ReferenceNumber and rlx.GuestNumber = r.GuestNumber
inner join RESERVATIONROOMLINE rl (nolock) on rlx.LINENUMBER = rl.LINENUMBER
and ISNULL(c.CardString, '') <> '' 
and ISNULL(rl.KeyPinCode, '') = ''
and status < 6

SELECT LineNumber,
convert(int, convert(binary(2),Hex, 1)) as Pincode
INTO #T2
FROM #T1

update reservationroomline
set KeyPincode = #T2.Pincode
FROM ReservationRoomline rl (NOLOCK)
INNER JOIN #T2 ON rl.LINENUMBER = #T2.LINENUMBER

drop table #T1
drop table #T2