DataSet查询生成器中的DATEDIFF

时间:2017-03-30 13:58:22

标签: sql dataset query-builder

如何在查询构建器中使用DATEDIFF?我试图计算插入时的年龄。

我在SqlQuery

中完成了这个工作插入代码
    INSERT INTO [dbo].[Employee]
           ([FirstName]
           ,[MiddleName]
           ,[LastName]
           ,[Age]
           ,[Gender]
           ,[Birthday]
           ,[PositionID]
           ,[Status]
           ,[ContactNumber]
           ,[Photo]
           ,[Password])
VALUES
           ('Lovenia'
           ,'Celestial'
           ,'Roman'
           ,(SELECT DATEDIFF(hour,'5-28-1998',GETDATE())/8766)
           ,'Male'
           ,'01-28-1998'
           ,'2'
           ,'Employed'
           ,'09128773636'
           ,null
           ,'somepassword')
GO

然后当我将其转换为查询生成器时,会弹出一个错误,并且说没有子查询允许

这是QueryBuilder中的代码

    INSERT INTO Employee
                         (FirstName, MiddleName, LastName, Age, Gender, Birthday, PositionID, Status, ContactNumber, Photo, Password)
VALUES        (@FirstName,@MiddleName,@LastName, (Select DATEDIFF('hour',@Birthday, GETDATE()) / 8766),@Gender,@Birthday,@PositionID,@Status,@ContactNumber,@Photo,@Password)

我删除了select语句并省略了DATEDIFF(hour,'5-28-1998',GETDATE())/8766

但仍然出现错误

我读到“查询”构建器中不支持DATEDIFF(),那么有没有其他方法(类似于DATEDIFF())来计算年龄?

1 个答案:

答案 0 :(得分:0)

没关系,我想通了

错误发生在DATEDIFF()的参数1,我删除了qoutes并将其更改为hh,同时修复了从字符串到日期的转换:D

这是代码

INSERT INTO Employee (FirstName, MiddleName, LastName, Age, Gender, Birthday, PositionID, Status, ContactNumber, Photo, Password)
VALUES        (@FirstName,@MiddleName,@LastName, DATEDIFF(hh, CONVERT(varchar(30),@Birthday, 102), GETDATE()) / 8766,@Gender,@Birthday,@PositionID,@Status,@ContactNumber,@Photo,@Password)