2013-06-06 127 views
2

我需要DATETIME2轉換爲SMALLDATETIME能有人給我如何DATETIME2轉換爲SMALLDATETIME觸發DATETIME2轉換爲smalldatetime觸發

一個簡單的例子,我想一個導火索是做到這一點的方式,但沒有得到的 語法正確。有人可以告訴我如何?我以前從未寫過 觸發器。

+0

@wterbeek 申報@ DT SMALLDATETIME,@ dtTime 選擇@ dtTime = inserted.BIRTH 選擇@ DT = CONVERT(SMALLDATETIME,@ dtTime,103) 選擇@ dt – kapozade

+0

檢查下面的答案 –

回答

2
DECLARE @datetime2 datetime2 = '12-10-25 12:32:10.1234567'; 
DECLARE @smalldatetime smalldatetime = @datetime2; 

SELECT @datetime2 AS '@datetime2', @smalldatetime AS '@smalldatetime'; 

結果:

> @datetime2     @datetime 
> --------------------------- ----------------------- 
> 2025-12-10 12:32:10.1234567 2025-12-10 12:32:10.123 
> 
> (1 row(s) affected) 
+0

我需要在插入數據時更改數據的類型。 我寫了這個代碼,但不工作 CREATE TRIGGER CustomerTrigger ON [GENIUS3] [CUSTOMER_EXTENSION] 對於插入 AS BEGIN DECLARE @ dtTime DATETIME2 SELECT @ dtTime = ID_DATE_OF_BIRTH爲由插入 SELECT CAST(@ dtTime AS SMALLDATETIME ) END – kapozade

+2

CREATE TRIGGER CustomerTrigger ON [GENIUS3]。[CUSTOMER_EXTENSION] FOR INSERT AS BEGIN DECLARE @ dtTime datetime2 SELECT @ dtTime = ID_DATE_OF_BIRTH FROM INSERTED SELECT @dtTime AS smalldatetime END檢查此 –

+0

感謝您的幫助。它正在工作 – kapozade