i need to check for a specific DateTime value in my table from my code (VB.NET) and i don’t know how to format the DateTime as a string. i’ve read that SQL Server will recognize my string if it’s in either date, time, or date and time format. i mean:
'May 15, 2004'
'5/15/2004'
'20040515'
'2004 4 am'
will SQL Server recognize these strings as valid DateTime values? i’m curious because if i check the actual DateTime values in the table they are in this format:
2/2/2006 3:49:33 PM
Don’t put the date/time value in the SQL query in the first place – use a parameterized query and then you don’t need to know or care what format SQL Server would parse literals as. You put the placeholder in the SQL, and specify the value as a
DateTimein the parameter collection.You should be using parameterized SQL as a matter of course, in fact – not only does it get rid of formatting and parsing problems like this, but possibly more importantly it’s the single most effective weapon against SQL injection attacks.