Question : Casting integer into date value within dynamic sql string

Hello experts i am trying to cast 3 integer fields into a datevalue then select all the record between a start and end date.  I am using vb.net to dynamically build the string, but i cant get it to work.  sday and eday are date properties that store a start and end date in this format dd/MM/yyyy.

   ssql = "SELECT Date, " & QTYColName & ", Year, Month, Day, Hour, Minute FROM LOG_SMALLPARTS Where (CAST(Day + " / " + Month + " / " + Year AS datetime) between " & sday & " and " & eday & ") "
     

Answer : Casting integer into date value within dynamic sql string

Hi,

Please try this:

ssql = "SELECT Date, " & QTYColName & ", Year, Month, Day, Hour, Minute FROM LOG_SMALLPARTS Where (RIGHT('0' + CAST(Year AS VARCHAR), 2) + '-' + RIGHT('0' + CAST(Month AS VARCHAR), 2) + '-' + RIGHT('0' + CAST(Day AS VARCHAR), 2) between '" & sday & "' and '" & eday & "') "

You need single quotes around the dates you're passing in.
Also, this code above will build your 3 columns Day, Month and Year into a formatted yyyy-mm-dd date (ISO standard). I'd recommend you also make sure sday and eday are passed in in this format also

HTH
Random Solutions  
 
programming4us programming4us