0
0

【Oracle DB】DATE型のカラムに日付をINSERTすると、ORA-01861: literal does not match format stringとなる

Posted at

やったこと

Goのdatabase/sqlパッケージを使用し、DATE型のカラムtest_dayに日付をINSERTした。

query := INSERT INTO my_table(test_day) VALUES('1999-01-01')

db.ExecContext(ctx, query)

エラー

ORA-01861: literal does not match format string

対応

OracleDBのTO_DATE関数を使用する
明示的にDATE型に変換して日時データにする必要がある。

query := INSERT INTO my_table(test_day) VALUES(TO_DATE('1999-01-01','YYYY-MM-DD'))

db.ExecContext(ctx, query)

こちらでも可能だが、時刻の指定はできず、時刻は常に00時00分00秒となる
日付リテラルであり、日時リテラルではない)

query := INSERT INTO my_table(test_day) VALUES(DATE '1999-01-01')

db.ExecContext(ctx, query)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0