HeidiSQL 将 varchar 转换为 Datetime

HeidiSQL Convert varchar into a Datetime

我继承了一个包含 varchars 而不是 datetimes 的数据库,我希望能够做一些报告,说明一个日期是否小于另一个。

我有这个数据库查询:

Select 
    RENEWDATE, 
    (CONCAT(substring(renewdate, 1,4), "-", substring(renewdate, 5,2), "-", substring(renewdate, 7,2), " 23:59:59")) 
FROM solutionsdmp 
WHERE  Approval = "Approved" 
GROUP BY Author 
ORDER BY  Author ASC

将此日期格式:20160217 转换为:2016-02-17 23:59:59

我有两个日期,一个是最后修改日期,一个是更新日期。它们都存储为 varchars 但格式不同。一个存储为 varchar,但格式为 datetime 2015-02-17 23:59:59,然后上面列出的另一个显示为 20160217。我希望能够将它们都转换为日期时间,以便我可以 运行 查询并检查一个是否小于另一个。这样我就可以看到续订日期是否已过。

我尝试了很多变体都不起作用,但这是我最近的尝试:

    SELECT 
        RENEWDATE, 
        CONVERT(DATETIME,(CONCAT(substring(renewdate, 1,4), "-", substring(renewdate, 5,2), "-", substring(renewdate, 7,2), " 23:59:59"))) 
    FROM solutionsdmp 
    WHERE  Approval = "Approved" 
    GROUP BY Author 
    ORDER BY  Author ASC

有人能给我指出正确的方向吗?

谢谢!

我自己想出了一个变通解决方案,我想我会post在这里为任何需要帮助的人提供帮助!

我做了一个 CAST,然后使用 HAVING 进行过滤。它对我有用!

Select 
     Author, 
     Problem,
     CAST(Last_Modified_DATE AS DATETIME) AS lmd, 
     CAST((CONCAT(substring(renewdate, 1,4), "-", substring(renewdate, 5,2), "-", substring(renewdate, 7,2), " 23:59:59")) AS DATETIME) AS rend
from solutionsdmp
WHERE Approval = "Approved"
HAVING 
lmd < rend
order by 
Author ASC