简体   繁体   English

将MySQL时间戳字段转换为SQL Server 2005日期字段

[英]Convert MySQL timestamp field to SQL Server 2005 Date field

I have been Googling this for more than an hour now, without any luck. 我已经在谷歌上搜索了一个多小时,没有任何运气。

Imported a MySQL database via CSV into MS SQL Server 2005, where a Tstamp (timestamp) field has been hassling me. 通过CSV将MySQL数据库导入到MS SQL Server 2005中,其中Tstamp(时间戳)字段一直困扰着我。

How do I convert the tstamp field into a SQL date field? 如何将tstamp字段转换为SQL日期字段?

Select *,
  DATEADD(SECOND, field8047, 1970/01/01) as datetime_created_calc
  From [Majestic].[dbo].[hdiyouth]

New error message: Msg 8116, Level 16, State 1, Line 1 Argument data type varchar is invalid for argument 2 of dateadd function. 新的错误消息:消息8116,级别16,状态1,行1参数数据类型varchar对于dateadd函数的参数2无效。

I guess that field8047 is a varchar field. 我猜field8047是一个varchar字段。

Try this: 尝试这个:

Select *,
  DATEADD(SECOND, cast(field8047 as int), '19700101') as datetime_created_calc
  From [Majestic].[dbo].[hdiyouth]

Assuming you have a VARCHAR() that represents a numeric type, and that numeric type represents the number of seconds from a fixed date. 假设您有一个VARCHAR()表示数字类型,并且该数字类型表示自固定日期起的秒数。

CAST your VARCHAR() into a numeric type. 将您的VARCHAR()转换为数字类型。

Then use that number in DATEADD(). 然后在DATEADD()中使用该数字。

If the number is too big, break it up into DAYS and SECONDS. 如果数字太大,则将其细分为DAYS和SECONDS。

SELECT
  DATEADD(
    DAYS,
    CAST([hdiyouth].[Tstamp] AS BIGINT) / (60*60*24),
    DATEADD(
      SECONDS,
      CAST([hdiyouth].[Tstamp] AS BIGINT) % (60*60*24),
      0
    )
  )
FROM
  [Majestic].[dbo].[hdiyouth]

If it's giving the right time, but the wrong date, change the 0 to whatever date your timestamp should be based from. 如果给出的时间正确,但日期错误,则将0更改为时间戳记的起始日期。

cast your field to integer. 将字段转换为整数。

Select  DATEADD(SECOND, CAST(field8047 AS INTEGER), '1970/01/01')
Select  DATEADD(SECOND, CAST('60' AS INTEGER), '1970/01/01')

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

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