0

I have problem with datetime format for C# and T-SQL.

  • C# application is using DD.MM.YYYY HH.mm.SS format
  • T-SQL is using MM\DD\YYYY HH.mm.SS format

Obviously T-SQL returns Error converting data type nvarchar to datetime. error.

Is there a way how to check and transform datetime format on SQL side? I would like T-SQL to be able to use DD.MM.YYY format.

Thanks.

1
  • 1
    T-SQL doesn't use any string formatting - a DateTime is a DateTime is a DateTime and doesn't have any formatting associated with it. You should use either parametrized queries, or if you really must have a date in string format, use the ISO-8601 standard format (YYYYMMDD or YYYYMMDDTHH:MM:SS) Commented Nov 13, 2011 at 20:57

1 Answer 1

4

Use parameterized queries to avoid this kind of issue.

When you build your SQL as a string to pass to SQL Server, you not only encounter this kind of issue, but may also be exposing yourself to SQL Injection.

With parameterized queries, you would be passing in a DateTime value and the DB library would do the right thing.

Formats don't come into play as they are only used for display.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using parameters: sCommand.Parameters.AddWithValue("@datetime_from", DatetimeFrom);
@feronovak - What type is DatetimeFrom? Can you post your full code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.