简体   繁体   English

如何在 SQL 查询中将日期转换为时间戳?

[英]How to convert date into timestamp in SQL query?

I'm trying to migrate data from old_table to new_table , but in old table the date datatype is datetime and in new table the date datatype is int and is accepting timestamp value.我正在尝试将数据从old_table迁移到new_table ,但在旧表中,日期数据类型是datetime ,在新表中,日期数据类型是int并且正在接受时间戳值。

So how to convert the old date in sql query so that it get inserted in new table as timestamp?那么如何转换 sql 查询中的旧日期,以便将其作为时间戳插入到新表中?

INSERT INTO `new_table` (`id`, `user_id`, `doctor_id`, `message_id`, `type`, `is_message`, `is_note`, `doctor_initials`, `call_status`, `message`, `date_created`, `date_updated`, `day`)
  SELECT id, usid, drid, message_id, type, is_message, is_note, doctor, kall, message, datein, 123, ziua
  FROM `old_table`;

I want a function which convert old date to value to timestamp eg in the above query CONVERT_INTO_TIMESTAMP(datein)我想要一个 function 将旧日期转换为值到时间戳,例如在上面的查询中CONVERT_INTO_TIMESTAMP(datein)

Any help will be highly appreciated.任何帮助将不胜感激。

New Table date_created field accepts values in unix timestamp such as: 1540642765新表date_created字段接受 unix 时间戳中的值,例如: 1540642765

Thanks谢谢

The function you are looking for is UNIX_TIMESTAMP() , eg 您要查找的函数是UNIX_TIMESTAMP() ,例如

select UNIX_TIMESTAMP('1970-01-01 00:00:00');

gives 0 in the UTC timezone. 在UTC时区给出0。

Try UNIX_TIMESTAMP() function: 尝试UNIX_TIMESTAMP()函数:

INSERT INTO `new_table`
            (`id`,
             `user_id`,
             `doctor_id`,
             `message_id`,
             `type`,
             `is_message`,
             `is_note`,
             `doctor_initials`,
             `call_status`,
             `message`,
             `date_created`,
             `date_updated`,
             `day`)
SELECT id,
       usid,
       drid,
       message_id,
       type,
       is_message,
       is_note,
       doctor,
       kall,
       message,
       Unix_timestamp(datein),
       '123',
       ziua
FROM   `old_table`;  

From Docs: 从文档中:

If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer. 如果不带任何参数调用,则以无符号整数形式返回Unix时间戳(自1970年1月1日00:00:00 UTC以来的秒数)。 If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. 如果使用日期参数调用UNIX_TIMESTAMP(),则它将返回自1970年1月1日00:00:00 UTC以来的参数值(以秒为单位)。 The date argument may be a DATE, DATETIME, or TIMESTAMP string, or a number in YYMMDD, YYMMDDHHMMSS, YYYYMMDD, or YYYYMMDDHHMMSS format. date参数可以是DATE,DATETIME或TIMESTAMP字符串,也可以是YYMMDD,YYMMDDHHMMSS,YYYYMMDD或YYYYMMDDHHMMSS格式的数字。 The server interprets date as a value in the current time zone and converts it to an internal value in UTC. 服务器将日期解释为当前时区中的值,并将其转换为UTC中的内部值。

SELECT date_part('epoch', CURRENT_TIMESTAMP)::int;

since this question is also tagged for SQL people who came here for timestamp solution in SQL/MSSQL Query.因为这个问题也被标记为 SQL 来这里寻求 SQL/MSSQL 查询中的时间戳解决方案的人。 can try below可以试试下面

SELECT CAST(DueDate AS TIMESTAMP) "NewDueDate" 

where DueDate is column with having date like - YYYY-MM-DD HH:ii:ss .其中DueDate是具有类似日期的列 - YYYY-MM-DD HH:ii:ss if your column is in type varchar then still above syntax will work fine.如果您的列是 varchar 类型,那么上面的语法仍然可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM