Reputation: 17278
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?
Upvotes: 1
Views: 2725
Reputation: 60556
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.
Upvotes: 4
Reputation: 4695
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.
Upvotes: 1
Reputation: 62564
If you are talking about SqlDateTime
structure you can use its constructor which accepts a value of DateTime
type:
See MSDN: SqlDateTime Constructor (DateTime)
Upvotes: 1
Reputation: 1934
yourDateTime.toString("yyyy-MM-dd HH:mm:ss");
But using parameters would be better
Upvotes: 2