在插入的整数中添加以下0

时间:2012-08-10 17:52:26

标签: sql-server

我将3或4位整数插入到需要5位数字的表中。如何在4位数字后面添加0,并在传输时在前面跟随0到3位数字?

编辑 - 这是定义如下的触发器的一部分:

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 

将T2.C3视为引脚号

2 个答案:

答案 0 :(得分:1)

为什么需要存储?只需将其添加到查询中或创建一个执行此操作的视图:

SELECT RIGHT('0' + RTRIM(Number) + '0', 5) FROM dbo.TableName;

当然,您可以在将值放入表中时对值执行相同的操作(通过修改插入语句或使用触发器,如果​​您对DML的控制较少)。

答案 1 :(得分:0)

这可以接受吗?

declare @PINs as Table ( PIN Int )
insert into @PINs ( PIN ) values ( 123 ), ( 4321 ), ( 12345 ), ( 5000 )
select Left( Right( '0' + Cast( PIN as VarChar(5) ), 5 ) + '0', 5 )
  from @PINs

根据您的问题陈述,它要求PIN值大于99.