In my opinion, SQL Server should convert it implicity, without using
CONVERT and CAST[
^] function (explicity).
Try this:
DECLARE @sdate VARCHAR(30)
SET @sdate = '2/28/2014 12:00:00 AM'
SELECT MyDate
FROM (
SELECT CONVERT(DATETIME,'2/27/2014 12:00:00 PM') AS MyDate UNION ALL
SELECT CONVERT(DATETIME,'2/28/2014 12:00:00 AM') AS MyDate UNION ALL
SELECT CONVERT(DATETIME,'1/01/2014 12:00:00 AM') AS MyDate UNION ALL
SELECT CONVERT(DATETIME,'12/31/2014 12:00:00 AM') AS MyDate UNION ALL
SELECT CONVERT(DATETIME,'2/22/2014 12:00:00 AM') AS MyDate
) AS T
WHERE MyDate <= @sdate
On the other hand, i would suggest you to use
stored procedure[
^] instead construct sql command in code, because of
SQL Injection[
^].
For further information, please see:
How to: Execute a Stored Procedure that Returns Rows[
^]
How To: Protect From SQL Injection in ASP.NET[
^]
Stop SQL Injection Attacks Before They Stop You[
^]
Use SearchBox at the top-right corner of this site to find more information about that.