我试图以秒为单位计算日期时间差。我的代码做什么->如果列A.KIOSKLASTUPDATED
为NULL,请获取当前时间。然后计算时间差。这部分正确完成。
现在我想添加if else语句以秒为单位的日期时间差异。如果差异是<= 300,请执行其他操作
我遇到错误:
关键字“ IF”附近的语法不正确。
我的代码:
SELECT
(SELECT TOP 1 CONCAT(B.OPERATORID, ' ', B.OPERATORNAME)
FROM ACTIVITY_LOG AS B
WHERE B.KIOSKID = A.KIOSKID
ORDER BY B.CREATED_AT DESC) AS CURRENTUSER,
IF(DATEDIFF(ss, ISNULL(CONVERT(VARCHAR(19), A.KIOSKLASTUPDATED, 20), CONVERT(VARCHAR(19), GETDATE(), 20)), CONVERT(VARCHAR(19), GETDATE(), 20)) <= 300)
BEGIN
SELECT '1' AS KIOSKSTATUS
END
ELSE
BEGIN
SELECT '0' AS KIOSKSTATUS
END
FROM
KIOSK_SETUP AS A
LEFT JOIN
DEPARTMENT AS C ON A.DEPARTMENTID = C.ID
答案 0 :(得分:1)
您想要一个case
表达式:
SELECT (SELECT TOP 1 CONCAT(B.OPERATORID,' ',B.OPERATORNAME) FROM ACTIVITY_LOG AS B WHERE B.KIOSKID = A.KIOSKID ORDER BY B.CREATED_AT DESC
) AS CURRENTUSER,
(CASE WHEN datediff(ss, ISNULL(CONVERT(VARCHAR(19), A.KIOSKLASTUPDATED, 20), CONVERT(VARCHAR(19), getdate(), 20)), CONVERT(VARCHAR(19), getdate(), 20)) <=300
THEN '1' ELSE '0'
END) AS KIOSKSTATUS
FROM KIOSK_SETUP A LEFT JOIN
DEPARTMENT C
ON A.DEPARTMENTID = C.ID;
也就是说,您在查询中还有许多其他问题:
A
和C
没有任何意义。IF()
适用于T-SQL代码,不适用于查询。