在sql中连接两个带有日期和时间的列?
我有两列作为在sql中连接两个带有日期和时间的列?,sql,sql-server,tsql,sql-server-2008,Sql,Sql Server,Tsql,Sql Server 2008,我有两列作为orderdate(03/02/2011)和ordertime(10.34 am),在这两列中,我必须连接这两列,并在另一列中显示为datetime值(03/02/2011 10:34:16.190) 有什么建议吗?您希望以编程方式或在sql server中输入: 在Sql Server中: 选择orderdate+ordertime作为“YourColumnName” 希望对您有所帮助。在MySQL中,它会像这样工作 SELECT Concat(orderdate, " ", or
orderdate(03/02/2011)
和ordertime(10.34 am)
,在这两列中,我必须连接这两列,并在另一列中显示为datetime值(03/02/2011 10:34:16.190)
有什么建议吗?您希望以编程方式或在sql server中输入: 在Sql Server中: 选择orderdate+ordertime作为“YourColumnName”
希望对您有所帮助。在MySQL中,它会像这样工作
SELECT Concat(orderdate, " ", ordertime) FROM vendors ORDER BY vend_name;
假设您使用的是
日期
和时间
,则需要进行转换才能添加
SELECT [orderdate] + CONVERT(datetime, [ordertime])
通常,解决方案是将天添加到时间值中,该时间值的天部分为零。您不清楚这两个值的数据类型,但是一个解决方案是:
With Inputs As
(
Select '20110302' As DateVal, '10:34 AM' As TimeVal
)
Select DateAdd(d, DateDiff(d, 0, Cast(DateVal As datetime)), Cast(TimeVal as datetime))
From Inputs
一个更明确的版本,假设输入是字符串,并给出确切的输入:
Set DateFormat MDY
With Inputs As
(
Select '03/02/2011' As DateVal, '10:34 AM' As TimeVal
)
Select DateAdd(d, DateDiff(d, 0, Cast(DateVal As datetime)), Cast(TimeVal as datetime))
From Inputs
-1您不能使用
+
运算符在SQL中添加日期和时间数据类型。但您可以将转换(varchar(10),orderdate,101)+''+转换(varchar(10),ordertime)作为“测试”。我刚刚给出了处理这个问题的方法。你也不会使用字符串。您的结果取决于语言您是如何从10:34 AM
的输入中获得10:34:16.190
的时间的?可能重复的