I have a stored proc that takes a date a string parameter. The data coming in looks like the form yyyy-MM-dd in the database the dates are save in the form 2012-02-26 06:00:00.000 (a time is attached to them. When I run the sql it always returns nothing because it cant find a comparison:
'select t_typday from ' + @dbName+ '.dbo.Calendar ' +
' where T_date LIKE ''' + @dateFilter + '%''';
You need to convert the date in your table to the
yyyy-mm-dd hh:mi:ss.mmmdate format before comparing it with your string parameter in that format usingLIKE:To fix your existing code with minimal changes, change
T_datetoCONVERT(VARCHAR(50),T_date,121), producing:If you do not perform the conversion, you are comparing a local format string (based on the
@@LANGUAGEsetting), such asApr 12 2012 12:00AMwith your string in@dateFilterwhich contains, for example,2012-04-12. This is why the comparison is not working.Of course passing in a parameter of type
DATEinto your stored procedure would be a much better solution, as several comments have already mentioned. That would allow you to compare the date inT_datewith the passed inDATEvalue in@dateFilterusing a simple expression such as: