I have a dateTime object in C# and i want to do an insert into SQL Server datetime field. What is the correct format for this?
The correct way to do this is by using a parameterised query, not text formatting. Then you can just use a strongly-typed SqlDbType.DateTime
parameter.
(If you absolutely must use text formatting to do this - and I strongly recommend against it - then something like yyyyMMdd HH:mm:ss
should do the trick.)
To expand on @Luke's answer I came across this bug just the other day.
The yyyy-MM-dd HH:mm:ss
format has a locale/language issue on SQL Server 2005 (an example is French), but is fixed in SQL 2008:
So, do NOT use this format: yyyy-MM-dd HH:mm:ss
(space separator).
Only use: yyyy-MM-ddTHH:mm:ss
("T" separator) or yyyyMMdd HH:mm:ss
(no dash delimiters)
Important if you're generating scripts that include datetime
constants.
See Jamie Thomson's article on SQL Blog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With