从字符串4转换datetime的语法错误

时间:2014-07-07 09:26:54

标签: c# sql datetime

执行以下代码时出现此代码错误。将不胜感激

DECLARE @Last_Timestamp DATETIME, 
        @New_Timestamp  DATETIME 

SELECT DISTINCT Rtrim(of_displayname) + ' (' 
                + Rtrim(ro_officer) + ')', 
                CONVERT(DATETIME, CONVERT(CHAR(11), ro_shift_start, 106), 106), 
                Rtrim(ac_name) + ' (' + Rtrim(ac_pin) + ')', 
                ro_officer 
FROM   roster WITH(nolock), 
       template WITH(nolock), 
       activity WITH(nolock), 
       officer WITH(nolock) 
WHERE  ro_status = 'INFO' 
       AND ro_activity IN ( 'LEAVE', 'SAL LEAVE' ) 
       AND ro_timestamp > @Last_Timestamp 
       AND ro_timestamp <= @New_Timestamp 
       AND ro_shift_start > Getdate() 
       AND ac_pin = to_activity 
       AND to_pin = ro_officer 
       AND Unicode(to_type) = Unicode('M') 
       AND ro_officer = of_pin 
       --AND (Of_Payroll_No    IN ('w','s')   
       AND ac_product_code IN ( '01', '09' ) /* Corporate Only */ /* W & S */ 
UNION 
SELECT [officer], 
       [activity], 
       [status], 
       [comment] 
FROM   production.rolecall.[dbo].[holidayrequest] 
ORDER  BY Rtrim(of_displayname) + ' (' 
          + Rtrim(ro_officer) + ')',RTRIM(Of_Displayname)  + ' (' + RTRIM(Ro_Officer) + ')',CONVERT(DATETIME,CONVERT(CHAR(11), RO_Shift_Start,106),106)

1 个答案:

答案 0 :(得分:0)

您在这里有3个地方进行日期转换:

  • CONVERT(DATETIME, CONVERT(CHAR(11), ro_shift_start, 106), 106)
  • CONVERT(DATETIME,CONVERT(CHAR(11), RO_Shift_Start,106),106)
  • UINON

前两个看起来像你实际上是在没有时间的情况下获得这一天。如果是这样的话:没有时间的那天;有多种方法可以做到这一点,但在最新版本的sql server中,将其转换为date可以正常工作:

select cast(columnname as date) as [columnname]

或者如果你真的需要datetime

 select cast(cast(columnname as date) as datetime) as [columnname]

在其他数据库版本上,您可以使用sneeky&#34;将其转换为浮动&#34;的方法:

select cast(floor(cast(columnname as float)) as datetime) as [columnname]

UNION更有趣;看起来这些是非常不同的形状 - 在这种情况下:你为什么要加入它们?我非常怀疑[activity]datetime,但这是它将成为其中一部分的列。此错误完全可能是因为尝试将[activity]转换为datetime