The value 4E5 is a result from a sql query and hence I cannot specify str before calling data.tables. Is there a parameter in data.table such as 'columntype' where I can specify the data type for the variables
This doesn't work as I am getting the value 4E5 from a sql query. Even if you tried to do str(4E5) it transforms it to a num. So your solution wouldn't work for my use case.
Well, you'll need to specify how you import the data in that case.
If you are reading the data directly from an obdc connection then the column type should be respected. If you are reading it from a file, then you can specify the string as part of the file read function.
Unfortunately I cannot replicate your second function (probably differences in allowed SQL syntax between our two systems).
One way to handle this may be to use the DBI and odbc packages rather than RODBC. (You may also consider dbplyr, too. I use them and have no such issues with column types. You can then always convert to a data.table for further manipulation once you have retrieved your data.