Converting varchar to smalldatetime results in a value out of range

Code:

strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"

seems to give me a 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. However, in a SQL database, transactiondate is a smalldatetime object.

I tried changing smalldatetime to datetime (in the database) and I tried transactiondate.toString (), but without success.

How can this be fixed?

Note. I am aware of the dangers of embedded SQL. I am looking for a quick solution to the solution here, and not a discussion of SQL injection.

+3
source share
4 answers

Try adding single quotes to the insert statement.

strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"
+6
source

Two possible answers:

  • You need to encapsulate your string in the apostrophe:

strSql = "insert into table2 (transactiondate) values ('" & transactiondate & "')"

  1. 1/1/1900 6/6/2079 ( smalldatetime)
+3

transactiondate.ToString( "yyyy/MM/dd HH: mm: ss" ) . , "" , , .

0

Smalldatetime > 1900 < 2079. , , ,

where year(datecolumn) < 2079
0

Source: https://habr.com/ru/post/1712328/


All Articles