Search code examples
c#sql-serverwinformssql-server-2005c#-4.0

Parse DateTime Parameter


I have a variable which is datetime type. How can i get the shortdatetostring() as datetime variable type ? I have a column in databae as datetime type. I would like to get the records which are added at a certain day.

Example:

 SELECT id FROM database WHERE added like @p1

The parameter of the query is a datetime variable.


Solution

  • Match based on day, month, and year of the date variables. Do not use strings, since matching is slow.

    SELECT id 
        FROM database 
        WHERE Datepart(yy, added) = Datepart(yy, @p1)
          AND  Datepart(mm, added) = Datepart(mm, @p1)
          AND Datepart(dd, added) = Datepart(dd, @p1)