Your format and your string don't agree.
You need a 0 in front of the 4, like this: 04/20/2012
.
Or you need to adjust your format, like this: M/D/YYYY
On second look, the to_date()
documentation doesn't show support for the single M
. You need to include the leading 0 with your month, and a single D
for the day would be extremely foolish.
An update based on your comment. Let me lead, though, by saying that using string concatenation to substitute data into queries like this is evil and likely to lead your app being hacked.
Dim queryString As String = "select * from smsdw.lot_act where (tran_dttm <= to_date( '" & bdp1.ToString("MM/dd/yyyy") & "','MM/DD/YYYY') and tran_dttm > to_date( '" & bdp1.ToString("MM/dd/yyyy") & "' ,'MM/DD/YYYY')"
As a bonus, doing this the right away avoids the need to worry about date formats at all:
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= :bdp1 and tran_dttm > :bdp1 "
command.Parameters.Add(":bdp1", SqlDbType.DateTime).Value = bdp1
But again: different libraries may handle this slightly different. Some require the :
character, some don't allow it, and some work on the order, rather than the name (meaning you would need to add the parameter twice).