我如何设置像年/月/ 1,2,3 ..即2015/04 / 1,2,3 ..的patientid格式?

时间:2015-04-06 10:33:05

标签: sql-server-2008 c#-4.0

我想设置像2015/04 / 1,2,3这样的patientid格式。我该怎么做?应该使用什么数据类型?月更改后,它会自动设置为新月,如2015/05 / 1,2,3。

1 个答案:

答案 0 :(得分:0)

我同意Jon的看法,你错过了很多背景,并且有许多你没有想过的事情,或者至少如果你认为它没有把它放在问题中。所以我相信这会转过来咬我,但这是试图回答这个问题。由于您使用的是MS SQL 2008,因此无法使用序列。

// a Sequence table
CREATE TABLE [dbo].[PatientId](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Junk] [nvarchar](10) NULL
) ON [PRIMARY]

GO

DECLARE @id INT;

// Insert a junk record to get the next identity value
INSERT INTO PatientId (Junk) VALUES ('');
// Store the value
SELECT @id = SCOPE_IDENTITY() 
// Format your poorly designed PatientId
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));

//Rinse and repeat
INSERT INTO PatientId (Junk) VALUES ('');
SELECT @id = SCOPE_IDENTITY() 
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));
INSERT INTO PatientId (Junk) VALUES ('');
SELECT @id = SCOPE_IDENTITY() 
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));
INSERT INTO PatientId (Junk) VALUES ('');
SELECT @id = SCOPE_IDENTITY() 
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));

TRUNCATE TABLE PatientId


INSERT INTO PatientId (Junk) VALUES ('');
SELECT @id = SCOPE_IDENTITY() 
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));
INSERT INTO PatientId (Junk) VALUES ('');
SELECT @id = SCOPE_IDENTITY() 
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));
INSERT INTO PatientId (Junk) VALUES ('');
SELECT @id = SCOPE_IDENTITY() 
SELECT LEFT(CONVERT(NVARCHAR(20), GETDATE(),111),7) +'/'+  CAST(@Id AS NVARCHAR(100));