DateTime in WHERE clause

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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

Ignacio Machin ( .NET/ C# MVP )

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top