Reputation:
The code:
strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"
seems to be giving me the runtime error:
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value
In the code, strSql is a String object and transactiondate is a Date object. In the SQL database however, transactiondate is a smalldatetime object.
I've tried changing the smalldatetime to a datetime (in the database) and I've tried transactiondate.toString() but with no success.
How can this be fixed?
Note: I know about the dangers of inline SQL. I am looking for a quick fix solution here and not a discussion about SQL injection.
Upvotes: 3
Views: 16692
Reputation: 680
Smalldatetime can only accept values for years > 1900 and < 2079 as well. I had a similar error to the one you posted where the solution was to filter out invalid years like such
where year(datecolumn) < 2079
Upvotes: 1
Reputation: 3886
Use transactiondate.ToString("yyyy/MM/dd HH:mm:ss") or your prefered date format. If it remains, change 'sa' user default language, or whatever user you use, to your prefered language.
Upvotes: 0
Reputation: 95123
Two possible answers:
strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"
smalldatetime
)Upvotes: 3
Reputation: 51468
Try adding single quotes to your insert statement.
strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"
Upvotes: 6