1

How does one convert a C# Datetime into the equivalent SQL DATE type, so that I can do a simple equality in a WHERE clase, with no parametisation?

4
  • 8
    Why don't you want to use parameters for that? Commented Apr 19, 2012 at 12:07
  • 3
    SQL parameters are safer and provide strong typing which can prevent a lot of typing errors in SQL queries. There's really not a good reason not to use them. Commented Apr 19, 2012 at 12:09
  • @Raubi, internal batch job and I have been told not to bother.... Commented Apr 19, 2012 at 12:11
  • 2
    By the time you have written this question, waited for the answer and understood it you could have used a parameter many times over. Commented Apr 19, 2012 at 12:24

4 Answers 4

4

Simply convert it to a iso string.

Date and Time

YourDateTime.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");

Only Date

YourDateTime.ToString("yyyy-MM-dd");

But i really encourage you to use parameterized queries because you will generate a new Sql Execution plan everytime your T-SQL query is different.

If you use Parameters you will get only one plan. This results in better performance.

More Information

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

Comments

2
yourDateTime.toString("yyyy-MM-dd HH:mm:ss");

But using parameters would be better

Comments

1

If you are talking about SqlDateTime structure you can use its constructor which accepts a value of DateTime type:

See MSDN: SqlDateTime Constructor (DateTime)

Comments

1

I think other answers didn't really understand your question (or I didn't)

From what I gathered you're asking how to convert from C# DateTime to SQLs Date type (not sql datetime!). All other answers included time in their answer.

If that's true, you don't have to worry about it at all. SQL Date is fully compatible with .NETs DateTime so just passing it to procedure will strip the time part and pass the date correctly.

Comments

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.