SQL Server日期比较引发错误

时间:2017-02-09 08:36:48

标签: sql sql-server

请帮助解决这个问题,独立查询如何工作,但下面的代码比较失败,我试图只比较日期而不是时间,所以尝试了这样的查询。

SELECT CAST(getdate() AS DATE)---'2/9/2017 12:00:00 AM'
SELECT CAST('2017/01/15' AS DATE)----'1/15/2017 12:00:00 AM'
SELECT CAST(getdate() AS DATE) > CAST('2017/01/15' AS DATE); -- Error SQLSTATE 42000

5 个答案:

答案 0 :(得分:2)

您需要将此条件置于上下文中。比如像这样

SELECT case when CAST(getdate() AS DATE) > CAST('2017/01/15' AS DATE) 
            then 1 
            else 0 
       end

答案 1 :(得分:1)

您可以尝试使用DATEDIFF()找出2个日期之间的差异。

选择DATEDIFF(DAY,(CAST(' 2017/01/15' AS DATE)),(CAST(getdate()AS DATE)))

答案 2 :(得分:1)

尝试这样的事情:

DECLARE @nowDate DATE
SELECT @nowDate = CAST(getdate() AS DATE)

DECLARE @otherDate DATE
SELECT @otherDate = CAST('2017/01/15' AS DATE)

SELECT DATEDIFF(DAY, @nowDate, @otherDate)

答案 3 :(得分:1)

$words = array("arif", "arin", "asif", "armin", "arpan");
$pattern = '|^ar|';

$indices = array();
foreach ($words as $i => $word) {
    // if there is a match
    if (preg_match($pattern, $word)) {
        // append the current index to the indices array
        $indices[] = $i;
    }
}

答案 4 :(得分:1)

您无法在READ_PHONE_STATE中进行比较,您可以使用以下代码来确定日期是否大于当前日期,或者今天是。

SELECT