DateAdd函数导致语法错误从字符串转换datetime

时间:2016-07-10 12:23:40

标签: c# asp.net sql-server

当我在sql server中执行以下查询时,从visual basic,它正确运行并返回我的数据。但是当我在我的代码中编写填充查询时,它会出现异常

  

从字符串

转换datetime的语法错误

我认为错误发生在where子句

这是我的疑问:

SELECT        Maint_TaskSheetDetails.TaskId, 
DATEADD(dd, (SELECT        MaintenanceRate
                                FROM            Maint_ItemTasks
                                WHERE        (TaskId = Maint_TaskSheetDetails.TaskId)), 
Maint_TaskSheets.SheetMaintenanceDate) AS Expr1, Maint_ItemTasks_5.TaskName, 
                         Maint_Items.ItemName, All_CostCenter.CostCenterName
FROM            Maint_TaskSheets INNER JOIN
                         Maint_TaskSheetDetails ON Maint_TaskSheets.TaskSheetId = Maint_TaskSheetDetails.TaskSheetId INNER JOIN
                         Maint_ItemTasks AS Maint_ItemTasks_5 ON Maint_TaskSheetDetails.TaskId = Maint_ItemTasks_5.TaskId INNER JOIN
                         Maint_Items ON Maint_TaskSheetDetails.ItemId = Maint_Items.ItemId INNER JOIN
                         All_CostCenter ON Maint_TaskSheets.CostCenterId = All_CostCenter.CostCenterID
**WHERE        (@Date1 <= DATEADD(dd,
                             (SELECT        MaintenanceRate
                                FROM            Maint_ItemTasks AS Maint_ItemTasks_3
                                WHERE        (TaskId = Maint_TaskSheetDetails.TaskId)), Maint_TaskSheets.SheetMaintenanceDate)) AND (@Date2 >= DATEADD(dd,
                             (SELECT        MaintenanceRate
                                FROM            Maint_ItemTasks AS Maint_ItemTasks_2
                                WHERE        (TaskId = Maint_TaskSheetDetails.TaskId)), Maint_TaskSheets.SheetMaintenanceDate))**
UNION ALL
SELECT        Maint_ItemTaskDetails.TaskId, DATEADD(dd,
                             (SELECT        MaintenanceRate
                                FROM            Maint_ItemTasks AS Maint_ItemTasks_1
                                WHERE        (TaskId = Maint_ItemTaskDetails.TaskId)), Maint_ItemTaskDetails.MaintenanceStartingDate) AS Expr1, Maint_ItemTasks_4.TaskName, 
                         Maint_Items_1.ItemName, All_CostCenter_1.CostCenterName
FROM            Maint_ItemTaskDetails INNER JOIN
                         Maint_ItemTasks AS Maint_ItemTasks_4 ON Maint_ItemTaskDetails.TaskId = Maint_ItemTasks_4.TaskId INNER JOIN
                         Maint_Items AS Maint_Items_1 ON Maint_ItemTasks_4.ItemId = Maint_Items_1.ItemId INNER JOIN
                         All_CostCenter AS All_CostCenter_1 ON Maint_ItemTaskDetails.CostCenterId = All_CostCenter_1.CostCenterID
**WHERE        (@Date1 <= DATEADD(dd,
                             (SELECT        MaintenanceRate
                                FROM            Maint_ItemTasks AS Maint_ItemTasks_1
                                WHERE        (TaskId = Maint_ItemTaskDetails.TaskId)), Maint_ItemTaskDetails.MaintenanceStartingDate)) AND (@Date2 >= DATEADD(dd,
                             (SELECT        MaintenanceRate
                                FROM            Maint_ItemTasks AS Maint_ItemTasks_1
                                WHERE        (TaskId = Maint_ItemTaskDetails.TaskId)), Maint_ItemTaskDetails.MaintenanceStartingDate))** AND (Maint_ItemTaskDetails.TaskId NOT IN
                             (SELECT        Maint_TaskSheetDetails_1.TaskId
                                FROM            Maint_TaskSheetDetails AS Maint_TaskSheetDetails_1 INNER JOIN
                                                         Maint_TaskSheets AS Maint_TaskSheets_1 ON Maint_TaskSheetDetails_1.TaskSheetId = Maint_TaskSheets_1.TaskSheetId))

1 个答案:

答案 0 :(得分:0)

尝试像这样转换或转换date1和date2。

CAST(@date1 as DATE)

您还可以将CONVERT()用于特定格式。