SQL 转换where子句中的日期格式

SQL Converting the date format in where clause

尝试使用 where exists 子句连接两个 table 时,由于日期的时间部分,结果不匹配。 table 1 中的日期是 2020-09-01 00:00:00,table 2 中的日期是 2020-09-01 12:54:00.

如何格式化日期以仅根据 YYYY-MM-DD 匹配?

SELECT * 
FROM  table1 a 
WHERE exists (
    SELECT '1' 
    FROM table2 b 
    WHERE a.company = b.company 
        AND a.emp =b.emp AND a.account_date = b.account_date
    )

在 Oracle 中,您可以使用 trunc():

SELECT * 
FROM table1 a 
WHERE exists (
    SELECT 1 
    FROM table2 b 
    WHERE 
        a.company = b.company 
        AND a.emp = b.emp 
        AND trunc(a.account_date) = trunc(b.account_date)
)

日期条件的表达方式往往更高效:

WHERE
    ...
    AND b.account_date >= trunc(a.account_date) 
    AND b.account_date < trunc(a.account_date) + 1