简体   繁体   中英

SQL Server 2012 date and time

I'm trying to create a simple database for a class but when entering time or date variables, I get an error. My code looks a little like this:

USE Music

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

Then I get the following error:

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

Length is stored as time(0) , ReleaseDate is stored as date , and DateAdded is stored as datetime2(4) .

Can anyone tell me how to correctly format these so my data will be applied?

Length has datatype of time , which can only store from 0 to 24 hours. But anyhow, I think you meant the album was 40 minutes and 17 seconds long, as opposed to 40 hours and 17 minutes long.

Try this:

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

You can try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM