Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error converting data type nvarchar to datetime

I have the following procedure interface:

Create procedure [dbo].[InsertItemDetails]
    @TimeItemAdded datetime

When I call it this way:

EXEC [dbo].[InsertItemDetails]
     @TimeItemAdded = N'20/07/2012 00:00:00';

I get this error:

Msg 8114, Level 16, State 5
Error converting data type nvarchar to datetime.

like image 503
fdgfdgs dfg Avatar asked Jul 25 '12 14:07

fdgfdgs dfg


2 Answers

Depending on your regional settings, the parameter you are passing in for @TimeItemAdded might not be recognized.

You should pass the date in as:

20120720
like image 190
LittleBobbyTables - Au Revoir Avatar answered Oct 07 '22 02:10

LittleBobbyTables - Au Revoir


Use an unambiguous string literal for your date. In this case:

EXEC dbo.InsertItemDetails
    ...
    , @TimeItemAdded = '20120720';

Better yet, make sure to pass a strongly typed parameter where you know that the date is correct. Ideally, this should never be presented as a string in any format.

Regional formats like m/d/y are bad news, because you can't ensure that they will work given the user's session, dateformat, language settings, the regional settings on the machine, etc.

like image 28
Aaron Bertrand Avatar answered Oct 07 '22 00:10

Aaron Bertrand