通过 SELECT 查询在 WHERE 中进行多个 AND | SQL 服务器 2012
Multiple AND in WHERE via SELECT Query | SQL SERVER 2012
我知道 AND 子句有限制,我想知道为什么这个查询不起作用?
SELECT
physicianmasterfile.idno,
physicianmasterfile.lastname,
physicianmasterfile.firstname,
physicianmasterfile.middlename,
physicianmasterfile.fingerscanno,
YEAR(CHECKINOUT.CHECKTIME) AS Year,
MONTH(CHECKINOUT.CHECKTIME) AS Month,
CONVERT(TIME(0),CHECKINOUT.CHECKTIME) AS Time
FROM
physicianmasterfile,
CHECKINOUT
WHERE
CHECKINOUT.userid=physicianmasterfile.fingerscanno AND
MONTH(CHECKINOUT.CHECKTIME) = MONTH(SYSDATETIME()) AND /* yields result in month(int) */
DAY(CHECKINOUT.CHECKTIME) = DAY(SYSDATETIME()) AND /* yields result in day(int) */
YEAR(CHECKINOUT.CHECKTIME) = YEAR(SYSDATETIME()) AND/* yields result in month(int) */
CHECKINOUT.CHECKTYPE='I'
当我试图删除其中一个 AND
时,查询运行没有问题,但是当有 5 个 AND
查询时,它在执行时没有产生任何结果。
有没有其他方法可以做到这一点而不会遇到错误?
我正在尝试获取 SYSDATETIME()
的值,它产生的值是 YYYY-MM-DD HH:MM:SS:MS 2019-07-22 09:25:31.9557058
。有没有办法只获取月份、日期和年份,并将时间分开?我要与之比较的数据 2015-07-27 08:00:01.000
,查询应该只显示 2019-07-22
.
中的所有数据
编辑:示例数据
CHECKTIME : 2015-07-27 08:00:01.000
可以用这个来比较日期 (111 = YYYY/MM/DD)?
WHERE
CHECKINOUT.userid=physicianmasterfile.fingerscanno AND
CONVERT(TIME(0), CHECKINOUT.CHECKTIME, 111) = CONVERT(TIME(0), SYSTDATEIME(), 111) AND
CHECKINOUT.CHECKTYPE='I'
您可以尝试将两个 datetime
(checktime
和 sysdatetime()
)单独转换为日期 - 这会截断它们的时间部分
... AND CONVERT(DATE, CHECKINOUT.CHECKTIME) = CONVERT(DATE, SYSDATETIME())
或
... AND CAST(CHECKINOUT.CHECKTIME AS DATE) = CAST(SYSDATETIME() AS DATE)
我知道 AND 子句有限制,我想知道为什么这个查询不起作用?
SELECT
physicianmasterfile.idno,
physicianmasterfile.lastname,
physicianmasterfile.firstname,
physicianmasterfile.middlename,
physicianmasterfile.fingerscanno,
YEAR(CHECKINOUT.CHECKTIME) AS Year,
MONTH(CHECKINOUT.CHECKTIME) AS Month,
CONVERT(TIME(0),CHECKINOUT.CHECKTIME) AS Time
FROM
physicianmasterfile,
CHECKINOUT
WHERE
CHECKINOUT.userid=physicianmasterfile.fingerscanno AND
MONTH(CHECKINOUT.CHECKTIME) = MONTH(SYSDATETIME()) AND /* yields result in month(int) */
DAY(CHECKINOUT.CHECKTIME) = DAY(SYSDATETIME()) AND /* yields result in day(int) */
YEAR(CHECKINOUT.CHECKTIME) = YEAR(SYSDATETIME()) AND/* yields result in month(int) */
CHECKINOUT.CHECKTYPE='I'
当我试图删除其中一个 AND
时,查询运行没有问题,但是当有 5 个 AND
查询时,它在执行时没有产生任何结果。
有没有其他方法可以做到这一点而不会遇到错误?
我正在尝试获取 SYSDATETIME()
的值,它产生的值是 YYYY-MM-DD HH:MM:SS:MS 2019-07-22 09:25:31.9557058
。有没有办法只获取月份、日期和年份,并将时间分开?我要与之比较的数据 2015-07-27 08:00:01.000
,查询应该只显示 2019-07-22
.
编辑:示例数据
CHECKTIME : 2015-07-27 08:00:01.000
可以用这个来比较日期 (111 = YYYY/MM/DD)?
WHERE
CHECKINOUT.userid=physicianmasterfile.fingerscanno AND
CONVERT(TIME(0), CHECKINOUT.CHECKTIME, 111) = CONVERT(TIME(0), SYSTDATEIME(), 111) AND
CHECKINOUT.CHECKTYPE='I'
您可以尝试将两个 datetime
(checktime
和 sysdatetime()
)单独转换为日期 - 这会截断它们的时间部分
... AND CONVERT(DATE, CHECKINOUT.CHECKTIME) = CONVERT(DATE, SYSDATETIME())
或
... AND CAST(CHECKINOUT.CHECKTIME AS DATE) = CAST(SYSDATETIME() AS DATE)