Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error converting data type nvarchar to datetime SQL Server

I am facing a problem

Error converting data type nvarchar to datetime

during inserting a date as a string literal in the format of 26/01/2017. I am using this code in SQL Server during insert:

CONVERT(DATETIME, @PaymentDate, 104)
like image 716
ahmed ansari Avatar asked Jan 26 '17 05:01

ahmed ansari


Video Answer


2 Answers

Try CONVERT(DATETIME, @PaymentDate, 103)

104 is the German style which uses periods between the numerals, as opposed to slashes. 103 is the British/French style.

See: https://msdn.microsoft.com/en-us/library/ms187928.aspx

like image 109
David Marchelya Avatar answered Sep 25 '22 00:09

David Marchelya


I've noticed your question is also tagged with c#.
If you are passing the date from c# to sql server, Don't pass dates as strings. Pass them as DateTime.
The .Net DateTime maps directly to SQL Server's DateTime.
This way, you will not have to deal with the display format at all, since both c# and SQL Server does not store display format in DateTime.

If you really need to convert the string '26/01/2017' to date, you should use 103 for your style argument, as already suggested in other answer.

like image 39
Zohar Peled Avatar answered Sep 24 '22 00:09

Zohar Peled