I need to insert dates which are formatted dd/mm/yyyy but unfortunately at the moment my sql is throwing up the following error from an embedded insert
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
I believe this is because the database is looking for the wrong date format. Can anyone point me to a solution to how I get the database to look for dd/mm/yyyy. I’d like to avoid converting every single date in the code because there are thousands of inserts done at the same time. Thanks
Don’t think about a particular format. Dates and times don’t inherently have formats. Make sure your database field is of a date/time-related type, and then use parameterized SQL which specifies
DateTimeorDateTimeOffsetvalues. That way your code doesn’t deal with strings at all.The fact that the error is talking about a conversion from
varcharsuggests that you’re supplying the data as strings. You shouldn’t be doing that. Even if your input format is text, you should be converting it into its natural data type (e.g.DateTimeOffset) as early as possible – it’ll make it a lot easier to work with the data throughout your codebase.