简体   繁体   English

Oracle时间戳到sql server DateTime

[英]Oracle timestamp to sql server DateTime

I have multiple statements from oracle database and I need to use them in SQL Server 我有来自oracle数据库的多个语句,我需要在SQL Server中使用它们

insert into COMENZI (NR_COMANDA, DATA, MODALITATE, ID_CLIENT, STARE_COMANDA, ID_ANGAJAT)
values (2456, to_timestamp('08-11-1998 07:53:25.989889', 'dd-mm-yyyy hh24:mi:ss.ff'), 'direct', 117, 0, 163);

insert into COMENZI (NR_COMANDA, DATA, MODALITATE, ID_CLIENT, STARE_COMANDA, ID_ANGAJAT)
values (2457, to_timestamp('01-11-1999 09:22:16.162632', 'dd-mm-yyyy hh24:mi:ss.ff'), 'direct', 118, 5, 159);

How can I create a function to_timestamp that returns a DateTime with the given value? 如何创建一个返回具有给定值的DateTime的函数to_timestamp?

The following works in SQL Server 2008 ( SQL Fiddle ): 以下适用于SQL Server 2008( SQL Fiddle ):

select convert(datetime, left(t, 10), 105) +
       convert(time, substring(t, 12, 12), 114)
from (select '01-11-1999 09:22:16.162632' as t) t;

Ironically, it doesn't work in SQL Server 2012. There, I think you have to do: 具有讽刺意味的是,它在SQL Server 2012中不起作用。我认为你必须这样做:

select dateadd(ms, datediff(ms, 0,  convert(datetime, substring(t, 12, 12), 114)),
               convert(datetime, left(t, 10), 105)
              )
from (select '01-11-1999 09:22:16.162632' as t) t;

Note in both cases, this uses milliseconds rather than microseconds. 请注意,在这两种情况下,这都使用毫秒而不是微秒。 I don't believe SQL Server offers date time value with that much precision. 我不相信SQL Server提供那么多精度的日期时间值。

If you change this statement 如果您更改此声明

select convert(datetime, left(t, 10), 105) +
       convert(time, substring(t, 12, 12), 114)
from (select '01-11-1999 09:22:16.162632' as t) t;

into

select convert(datetime, left(t, 10), 105) +
       convert(datetime, substring(t, 12, 12), 114)
from (select '01-11-1999 09:22:16.162632' as t) t;

it will work correct with all SQL-Server versions 它将适用于所有SQL-Server版本

The error that datetime and time are incompatible types in the add-operator occurs only in SQL-Server 2012. 日期 时间时间add-operator中不兼容类型的错误仅在SQL-Server 2012中发生。

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

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