我想用这样的东西更新另一个表的字段:
UPDATE table1
left join table2 on table1.key = table2.key
set table1.old_date = STR_TO_DATE(table2.string_date,'%Y%m%d %T');
SELECT table1.old_date, table2.string_date, STR_TO_DATE(string_date,'%Y%m%d')) as "new_dob"
left join table2 on table1.key = table2.key
我的old_date是一个日期时间字段,我的string_date是一个格式为"YYYYMMDD"的字符串日期当我进行选择时,我的日期格式正确,但是当我尝试更新时,它失败并显示错误:
Data Truncation error occurred on a write of column 0Data was 0 bytes long and 0 bytes were transferred.
我认为这是一个日期格式问题,但是根据这里的社区错误,我找不到真正的解决方案(我尝试了几个)。
谢谢
编辑:我在这里添加一些关于我的数据库的信息:
ColumnIndex getColumnName getColumnTypeName getPrecision getScale isNullable getTableName getSchemaName getCatalogName getColumnClassName getColumnDisplaySize getColumnLabel getColumnType isAutoIncrement isCaseSensitive isCurrency isDefinitelyWritable isReadOnly isSearchable isSigned isWritable
1 string_date VARCHAR 8 0 1 dump_external_customer my_db java.lang.String 8 string_date 12 false false false true false true false true
2 old_date DATETIME 19 0 0 customer my_db java.sql.Timestamp 19 old_date 93 false false false true false true false true
19750921
1970-08-17 00:00:00
我的转换日期1975年09月21日
如果格式为"YYYYMMDD",则没有时间组件。 所以不要在str_to_date()
中寻找它:
UPDATE table1 left join
table2
on table1.key = table2.key
set table1.old_date = STR_TO_DATE(table2.string_date, '%Y%m%d');
当然,MySQL确实可以将该格式识别为日期,因此您也可以仅使用date()
:
UPDATE table1 left join
table2
on table1.key = table2.key
set table1.old_date = DATE(table2.string_date);
更新表1在表 1 上左连接表 2.key = 表 2.keyset table1.old_date = date_format(STR_TO_DATE(table2.string_date,
在 Squirrel 中使用 Oracle MySQL Connector/J 驱动程序,
这有效:
select date('2016-01-11 06:40:10.188918 UTC') as bbb;
但这失败了:
drop table if exists temp_junk;
create temporary table temp_junk as
select date('2016-01-11 06:40:10.188918 UTC') as bbb;
错误:
Data Truncation error occurred on a write of column 0Data was 0 bytes long and 0 bytes were transferred.
Error occurred in:
create temporary table temp_junk as
select date('2016-01-11 06:40:10.188918 UTC') as bbb
在SQL小提琴(http://sqlfiddle.com/#!9/5025b3/1/0)中,我至少得到了一条不错的错误消息(但只有使用CREATE语句,简单的SELECT仍然成功):
Data truncation: Truncated incorrect datetime value: '2016-01-11 06:40:10.188918 UTC'
也许你的错误是相似的。