我想从另一个表的字段中更新一个日期字段,如下所示:
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 true19750921
1970-08-17 00:00:00
我的转换日期1975-09-21
发布于 2016-03-04 20:34:23
如果格式为"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);发布于 2016-03-04 21:02:10
UPDATE table1 left join table2 on table1.key = table2.key set table1.old_date = date_format(STR_TO_DATE(table2.string_date,'%Y%m%d'),'%Y-%m-%d');
发布于 2016-04-01 04:46:35
在使用Oracle MySQL连接器/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 fiddle ( http://sqlfiddle.com/#!9/5025b3/1/0 )中,我至少得到了一条不错的错误消息(但只有在使用CREATE语句的情况下,简单的SELECT仍然成功):
Data truncation: Truncated incorrect datetime value: '2016-01-11 06:40:10.188918 UTC'也许你的错误也是类似的。
https://stackoverflow.com/questions/35796115
复制相似问题