2015-10-16 102 views
1

我想創建一個簡單的類的數據庫,但輸入時間或日期變量時,我得到一個錯誤。我的代碼看起來有點像這樣:SQL Server 2012的日期和時間

USE Music 

INSERT INTO [dbo].[Album] ([Length], ReleaseDate, DateAdded) 
VALUES ('40:17', '2015/09/18', '2015/10/15 00:00:00.0000') 

然後我得到以下錯誤:

Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.

Length存儲爲time(0)ReleaseDate存儲爲date,並DateAdded存儲爲datetime2(4)

任何人都可以告訴我如何正確格式化這些數據,因此我的數據將被應用?

回答

2

Length的數據類型爲time,它只能存儲0到24小時。但無論如何,我認爲你的意思是這張專輯長40分17秒,而不是40小時17分。

試試這個:

INSERT INTO [dbo].[Album] 
     ([Length], ReleaseDate, DateAdded) 
VALUES ('00:40:17', '2015/09/18', '2015/10/15 00:00:00.0000') 
0

你可以試試:

INSERT INTO [dbo].[Album] 
     ([Length], ReleaseDate, DateAdded) 
VALUES ('00:40:17', '20150918', '20151015')