在oracle中插入数据时不是有效的月份

时间:2016-09-29 11:49:40

标签: oracle

我使用以下查询将数据插入表中。

INSERT INTO xxcus.xxacl_pn_agrmnt_mst
        (mkey, transaction_type, survey_area_7_12, ref_date, status,
         mst_date, remarks, tran_type, created_by, creation_date,
         last_update_date, last_updated_by, delete_flag
        )
 VALUES (1, 'AGR', 'khan,', '29-09-2016', 'AGD',
         '11/09/2016', 'Test', 'AM', '5681', '29-09-2016 17:10:19',
         '29-09-2016 17:10:19', '5681', 'N'
        )

但是错误是

  

不是29-09-2016的有效月份

以下是我插入的代码

xw.WriteElementString("REF_DATE", txtRefdate.Value);

我不知道这里有什么问题

2 个答案:

答案 0 :(得分:2)

您应该按

投射日期列的数据类型
to_date('29-09-2016 17:10:19', 'DD-MM-YYYY HH24:MI:SS')

答案 1 :(得分:1)

'29-09-2016 17:10:19'不是字符串的日期。

Oracle will use the NLS_DATE_FORMAT session parameter as the format mask when implicitly converting a string to a date(即当您尝试将字符串值插入日期列时),如果此格式掩码与字符串的格式不匹配,那么您将收到错误。

要生成日期,您应该通过以下方式将字符串显式转换为日期:

您的查询应该是(如果您使用ANSI文字):

INSERT INTO xxcus.xxacl_pn_agrmnt_mst (
  mkey,
  transaction_type,
  survey_area_7_12,
  ref_date,
  status,
  mst_date,
  remarks,
  tran_type,
  created_by,
  creation_date,
  last_update_date,
  last_updated_by,
  delete_flag
) VALUES (
  1,
  'AGR',
  'khan,',
  DATE '2016-09-29',
  'AGD',
  DATE '2016-09-11',
  'Test',
  'AM',
  '5681',
  TIMESTAMP '2016-09-29 17:10:19',
  TIMESTAMP '2016-09-29 17:10:19',
  '5681',
  'N'
)