0

我已将csv文件导入到 sap hana,date列数据类型与inthana 表定义中的一样。我想将数据类型从intto更改为date并希望相应地对记录应用更改。

4

1 回答 1

0

If the date column has values in int form, they must be in the unix_timestamp form.

If you want to convert the column to date or datetime data type, you better add a new column with required data type. And then update the column as selected from old int column. Lastly you can drop the int column, and start using date type column.

-- add new datetime type column
ALTER TABLE table_name
  ADD COLUMN date_col_new_name DATETIME;

-- add values into the same column
-- read from int type date column
UPDATE table_name
   SET date_col_new_name = FROM_UNIXTIME( int_type_date_col );

-- now drop the int type date column
ALTER TABLE DROP COLUMN int_type_date_col;

Example: On UNIX_TIMESTAMP and FROM_UNIXTIME:

mysql> drop table if exists ut_xmple;
mysql> create table ut_xmple( date int );
mysql> insert into ut_xmple( date ) values ( unix_timestamp() );
mysql> select * from ut_xmple;
+------------+
| date       |
+------------+
| 1399536302 |
+------------+

mysql> alter table ut_xmple add column date_time datetime;
mysql> select * from ut_xmple;
+------------+-----------+
| date       | date_time |
+------------+-----------+
| 1399536302 | NULL      |
+------------+-----------+

mysql> update ut_xmple set date_time=from_unixtime( date );
mysql> select * from ut_xmple;
+------------+---------------------+
| date       | date_time           |
+------------+---------------------+
| 1399536302 | 2014-05-08 13:35:02 |
+------------+---------------------+

mysql> select now(), @ut:=unix_timestamp() ut, from_unixtime( @ut ) fut;
+---------------------+------------+---------------------+
| now()               | ut         | fut                 |
+---------------------+------------+---------------------+
| 2014-05-08 13:35:54 | 1399536354 | 2014-05-08 13:35:54 |
+---------------------+------------+---------------------+

Refer to:

于 2014-05-08T08:10:49.413 回答