I am inserting a 3 or 4 digit integer into a table that needs the number to be 5 digits long. How do I add a 0 following the 4 digit number and a leading and following 0 to the 3 digit numbers when they transfer?
Edit- This is part of a trigger defined as follows:
create trigger Trig on T2
after insert, update, delete
as
-- So that @@rowcount does not get affected by trigger code
set NoCount on
-- If c3 column participated in update (it most certainly did in insert)
if update(C3)
begin
-- Insert or update triggered this operation
if exists (select * from Inserted)
begin
insert into P1 (C1, C2, C3)
select T1.C2, T1.C3, T2.C3 --INFORMATION THAT NEEDS TO BE TRANSFERRED
from T1
-- Inserted is a pseudotable holding inserted/updated data
inner join inserted T2
on T1.C1 = T2.C2
where T2.C4 <> 9 --DETERMINES IF RECORD NEEDS TO BE TRANSFERRED
end
else
-- Separate insert for deleted records
-- To simplify inserting additional info for deleted records only
begin
insert into P1 (C1, C2, C3)
select T1.C2, T1.C3, T2.C3 --INFORMATION THAT NEEDS TO BE TRANSFERRED
from T1
-- deleted is a pseudotable holding deleted/updated data
inner join deleted T2
on T1.C1 = T2.C2
where T2.C4 <> 9 --DETERMINES IF RECORD NEEDS TO BE TRANSFERRED
end
end
Consider T2.C3 to be the pin number
Why do you need to store this? Just add it to the query or create a view that does so:
You can of course perform the same operation on the values as you’re putting them into the table (either by modifying your insert statement or using a trigger if you have less control over DML).