CREATE TABLE tbl_Patient
(
PatientID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
LabControlID AS Cast(Right(Year(getDate()),4) as varchar(4)) +'-' + RIGHT('00000' + CAST(PatientID AS VARCHAR(5)), 5),
SequenceID AS "--Codes to display date--"
,First_Name varchar(50)
,Last_Name varchar(50)
,Age int
,Male bit
,Female bit
,
)
我想显示月和日 - 00000并在下面的示例中显示如下。
SequenceID
----------
Jan01-00001
Jan01-00002
请指教。谢谢:D
答案 0 :(得分:7)
您应该重新考虑您的表格结构:
Age
很棘手(每年都会更新)吗?您应该存储birthday
并进行计算。Male bit
和Female bit
。如果没有检查约束,您可以获得male 1 and female 1
。如果需要,可能只有isMale bit
就足够了,或者查找表中包含更多值。LabControlID
使用GETDATE
,并且与时间有关。当年份发生变化时,它也会发生变化。对于SequenceId
,您可以使用FORMAT
并将date
存储为普通列:
CREATE TABLE tbl_Patient
(
PatientID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
my_date DATE,
SequenceID AS CONCAT(FORMAT(my_date,'MMMdd','en-US'),'-',FORMAT(PatientID,'0000#'))
-- ...
);
INSERT INTO tbl_Patient(my_date)VALUES ('2016-01-01'), ('2016-01-01');
SELECT * FROM tbl_Patient;
的 LiveDemo
强>
输出:
╔═══════════╦═════════════════════╦═════════════╗
║ PatientID ║ my_date ║ SequenceID ║
╠═══════════╬═════════════════════╬═════════════╣
║ 1 ║ 01.01.2016 00:00:00 ║ Jan01-00001 ║
║ 2 ║ 01.01.2016 00:00:00 ║ Jan01-00002 ║
╚═══════════╩═════════════════════╩═════════════╝