简体   繁体   English

SQL Server连接日期和时间

[英]SQL server concatenate date and time

I have two columns which are "datetime" type in SQL server. 我有两列是SQL Server中的“日期时间”类型。 However, one columns contains the valid date I need and the other one contains the time(hour, min,..) only. 但是,一列包含我需要的有效日期,而另一列仅包含时间(小时,分钟,..)。

How can I extract the date from the first column and the time from the last column and concatenate them together into a valid datetime column. 如何从第一列中提取日期,从最后一列中提取时间,并将它们连接在一起成为有效的datetime列。

What I have tried: 我尝试过的

select 
[Created on],
[Created At],
LEFT([Created On],11),
RIGHT([Created At],8),
LEFT([Created On],11) + RIGHT([Created At],8)
from.. 

However, the output makes sense but I want it to be a valid timestamp type in military format as the first and second columns. 但是,输出有意义,但我希望它成为军事格式的有效时间戳类型,如第一列和第二列。 How can I do it? 我该怎么做?

在此处输入图片说明

I did some google and this solution below works for me. 我做了一些谷歌和下面这个解决方案对我有效。

select 
[Created on],
[Created At],
CAST(Convert(date, [Created on]) as datetime) + CAST(Convert(time, [Created At]) as datetime),
LEFT([Created On],11) + RIGHT([Created At],8)
from [VERICAL$Purch_ Inv_ Header] 
where [Posting Date] > '2014-02-01

在此处输入图片说明

try using this expression:- 尝试使用此表达式:-

select
DATEADD(MILLISECOND,datepart(MILLISECOND,created_at),
DATEADD(SECOND,datepart(SECOND,created_at),
DATEADD(minute,datepart(minute,created_at),
DATEADD(HOUR,datepart(hour,created_at),created_on))))
from ..

It actually finds the time part from the created at column and adds to created on 实际上,它从created at列中找到时间部分,并添加到created on

Grab relevant pieces of date and time from the appropriate column using DATEPART , and piece back together using DATETIMEFROMPARTS (if on SQL Server 2012). 使用DATEPART从相应的列中获取相关的日期和时间,并使用DATETIMEFROMPARTS一起归零 (如果在SQL Server 2012上)。

SQLFiddle SQLFiddle

Should be able to just add them, + 应该可以添加它们,+

select 
[Created on],
[Created At],
LEFT([Created On],11),
RIGHT([Created At],8),
[Created On] + [Created At] as whatever

The cleanest way to do it, in my opinion, is to make use of how datetime is implemented by SQL Server: SQL Server implements a datetime value as a pair of signed 32-bit integers. 我认为,最干净的方法是利用SQL Server如何实现datetime时间:SQL Server将datetime值实现为一对带符号的32位整数。

  • The high-order word is the date component. 高阶单词是日期部分。 It is the offset in days from the SQL Server's epoch of 1 January 1900. 它是距1900年1月1日的SQL Server时代的天数偏移量。

  • The low-order word is the time component. 低阶字是时间成分。 It is the offset in milliseconds from start-of-day (00:00:00.000). 它是从一天的开始(00:00:00.000)的偏移量(以毫秒为单位)。

With that knowledge in hand, we can see the implementation thus: 掌握了这些知识之后,我们可以看到实现:

select convert(binary(8),convert(datetime,'March 17, 2014'))

returns 回报

0x0000A2F100000000

and

select convert(binary(8),convert(datetime,'17:55:23')) 选择convert(binary(8),convert(datetime,'17:55:23'))

returns 回报

0x0000000001275CE4

So, to meld the date component of one datetime value with the time component of another: 因此,要将一个datetime值的日期部分与另一个datetime值的时间部分融合在一起:

convert(datetime,
    substring(convert(binary(8),date_only_column),1,4)
  + substring(convert(binary(8),time_only_column),5,4)
  )

Easy! 简单!

And to format as an ISO 8601 string: 并格式化为ISO 8601字符串:

convert(varchar(32),
  convert(datetime,
      substring(convert(binary(8),date_only_column),1,4)
    + substring(convert(binary(8),time_only_column),5,4)
    ) ,
    120 -- or 121 if your want milliseconds.
  )

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

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