DateTime in WHERE clause

  • Thread starter Thread starter Roy
  • Start date Start date
R

Roy

I want to dynamically construct a TSQL with a datetime column in the WHERE
clause. How do I include the millisecond in the query?
"WHERE datetimecolumn = " + dt.ToString() will not include the millisecond.
Thanks.
 
Roy,

You don't want to do it like this. Instead, when creating your
SqlCommand instance, use a parameterized query. Your where clause will look
like this:

WHERE datetimecolumn = @datetimevalue

And then you will add the @datetimevalue parameter through the
parameters collection on the command (passing the value you are looking to
insert for that parameter as well).

The provider will do the conversion for you.
 
I want to dynamically construct a TSQL with a datetime column in the WHERE
clause. How do I include the millisecond in the query?
"WHERE datetimecolumn = " + dt.ToString() will not include the millisecond.
Thanks.

Hi,

There are two options:
1- Use "WHERE myDate='"+ DateTime.Today.ToShortDateTime() + "'";

or better yet, use a parameter.
 
Back
Top