尝试从varchar(50)转换为datetime,今天的日期+只有小时分和秒

时间:2017-03-08 04:54:59

标签: sql datetime time

我有一个临时表,我试图将下面显示的日期转换为datetime。该列设置为varchar(50)。数据是从长度为50个字符的csv文件传输的。

[CODE]
SELECT [Date]
  ,Patient
  ,ISNULL(Doctor,-1) AS Doctor
  ,[Procedure]
  ,CAST(Charge AS decimal(8,2)) AS ChargeDecimal 
FROM ##TempBellevueLoadCSV

--Result:
Date
00:00.0
15:00.0 --Shown below:
00:00.0
00:00.0

--Need Datetime format with today's date added like below: 
Date
2017-03-08 15:00:00.000
 SELECT 

     CAST('0000-00-00 '+ SUBSTRING(CAST(t.[Date] AS varchar(8)),1, 7)+'0.000'
               AS varchar) AS [Date]
     ,Patient 
     ,Doctor 
     ,[Procedure] 
     ,CAST(Charge AS decimal(8,2)) AS Charge 
 FROM ##TempBellevueLoadCSV as t

 --This looks like it, but cannot cast into datetime.

 --Could I do this??? Doesn't work.
 CAST(GETDATE()+[Date]) AS datetime

[/ CODE]

- 更新:我需要将当前日期添加小时分钟秒作为数据的一部分。如何将今天的日期添加到我已经拥有的小时,分​​钟,秒中?

2 个答案:

答案 0 :(得分:0)

SELECT CONVERT(DATETIME,[Date],103)[Date]
,Patient
,ISNULL(Doctor,-1) AS Doctor
,[Procedure]
,CAST(Charge AS decimal(8,2)) AS ChargeDecimal 
FROM ##TempBellevueLoadCSV

答案 1 :(得分:0)

我在添加派生列时使用SSIS解决了这个问题,取而代之的是时间和      添加一行代码:      [码]      (DT_WSTR,4)DATEPART(" yyyy",GETDATE())+右(" 0" +      (DT_WSTR,2)DATEPART(" mm",GETDATE()),2)+右(" 0" +      enter image description here      (DT_WSTR,2)DATEPART(" dd",GETDATE()),2)+时间      [/代码]