计算列规范

时间:2016-03-31 13:55:16

标签: vb.net sql-server-2014

CREATE TABLE PaperTable
(
    PaperID int IDENTITY (1,1) NOT NULL
 , PageNumber varchar(100)
  , PaperAlphaID AS Cast(Right(Year(getDate()),2) as varchar(2)) +'-'+ PageNumber
);

当我添加" 001"和" 002"在我的论文编号中:

PaperID   PageNumber  PaperAlphaID
-------   ----------  ------------
  1         001          15-001
  2         002          15-002

但我想在PaperAlphaID中显示全年

PaperID   PageNumber  PaperAlphaID
-------   ----------  ------------
  1         001          2015-001
  2         002          2015-002

而不是15-001和15-002

1 个答案:

答案 0 :(得分:3)

删除RIGHT功能并更新varchar(4)

CREATE TABLE PaperTable
(
   PaperID int IDENTITY (1,1) NOT NULL
   , PageNumber varchar(100)
   ,   PaperAlphaID AS Cast(Year(getDate())) as varchar(4)) +'-'+ PageNumber
);