user4814432
user4814432

Reputation: 41

Conversion failed when converting date and/or time from character string - Visual Studio

I'm trying to insert data into my database using these commands (Homework):

SET IDENTITY_INSERT [dbo].[Movies] ON

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (1, 3, N'Titanic', N'James Cameron', N'1997‐06‐21 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (2, 1, N'StarWars', N'George Lucas', N'1977-06‐01 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (3, 1, N'Jurassic Park', N'Steven Spielberg', N'1993‐06‐17 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (4, 4, N'Jaws', N'Steven Spielberg', N'1975-05-30 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (5, 3, N'Ghost', N'Jerry Zucker', N'1990‐06-­14 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (7, 3, N'Forrest Gump', N'Robert Zemeckis', N'1994‐06‐18 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (8, 2, N'Ice Age', N'Chris Wedge', N'2002‐06‐26 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title],[Director], [DateReleased]) 
VALUES (9, 2, N'Shrek', N'AndrewAdamson', N'2001‐06‐25 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title],[Director], [DateReleased]) 
VALUES (10, 1, N'Independence Day', N'Roland Emmerich', N'1996-06-20 00:00:00')

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (11, 4, N'The Ring', N'Gore Verbinski', N'2002‐06‐26 00:00:00')

SET IDENTITY_INSERT [dbo].[Movies] OFF

But it keeps giving me the exception:

Conversion failed when converting date and/or time from character string

Upvotes: 3

Views: 894

Answers (2)

marc_s
marc_s

Reputation: 754220

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.

The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.

The ISO-8601 format is supported by SQL Server comes in two flavors:

  • YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!

or:

  • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.

This is valid for SQL Server 2000 and newer.

If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.

Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.

The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible

So in your sample code - does this insert work? (I added the T separator between the YYYY-MM-DD date and the time portion)

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (11, 4, N'The Ring', N'Gore Verbinski', N'2002‐06‐26T00:00:00')

Or if you're only interested in the date itself (not the time) - try this:

INSERT INTO [dbo].[Movies] ([Id], [CategoryId], [Title], [Director], [DateReleased]) 
VALUES (11, 4, N'The Ring', N'Gore Verbinski', N'20020626')

Upvotes: 1

alerya
alerya

Reputation: 3175

Try this:

'2002‐06‐26 00:00:00.000'

Upvotes: 0

Related Questions