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)
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.
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.
The issue I faced was that the date was having the correct format but the time part was incorrect, the date was like
07/28/2021 00:00 PM
where 00 hours cannot be possible in PM, that is why DB was not accepting them while saving the date/time. so I converted the date to
07/28/2021 00:00 AM
and it was saved. maybe helpful for anyone.
@PaymentDate? This related question may help here - stackoverflow.com/questions/11651585/…