如何在SQL Server中的两个日期之间获得差异

时间:2016-02-01 10:31:43

标签: sql-server sql-server-2008 sql-server-2005 sql-server-ce mssql-jdbc

SELECT 
    DATEDIFF(minute, CONVERT(VARCHAR, CURRENT_TIMESTAMP, 102), CONVERT(VARCHAR, t.xxxxxx, 102)) AS daydifference 
FROM 
    (SELECT MAX(autoid), xxxxxx 
     FROM Xtable 
     WHERE uid = 3) t;

我最后收到错误“t”。没有为't'的第1列指定列名。

3 个答案:

答案 0 :(得分:0)

试试这样。在t

中为列添加别名
SELECT  DATEDIFF(minute, CONVERT(VARCHAR, CURRENT_TIMESTAMP, 102),
                     CONVERT(VARCHAR, t.xxxxxx, 102)) AS daydifference
    FROM    ( SELECT    MAX(autoid)  as autoid ,
                        xxxxxx as [column_name]
              FROM      Xtable
              WHERE     uid = 3
            ) t;

答案 1 :(得分:0)

问题在于您的子查询。第一列没有名称。这是不允许的。替换:

<div ng-controller="freeUserController" class="form-group" id=existingUser>
    <label>Username</label> <select class="form-control select2"
        style="width: 100%;" name="user">
        <option ng-repeat="user in users" value="{{user.username}}">
        {{user.username}}</option>
    </select>
</div>

MAX(autoid)

答案 2 :(得分:0)

这会使您的查询有效。但是我真的不相信这就是你要找的东西:

SELECT 
    DATEDIFF(minute, CURRENT_TIMESTAMP, t.xxxxxx) AS daydifference 
FROM 
    (SELECT MAX(autoid) autoid, xxxxxx 
     FROM Xtable 
     WHERE uid = 3
     GROUP BY xxxxxx
     ) t;

注意:使用聚合函数时,您需要按所选列的分组。列需要以子选择命名。

不是100%肯定你在问什么。但这就是我认为你需要的东西:

SELECT TOP 1
    DATEDIFF(minute, 0, CURRENT_TIMESTAMP - xxxxxx) improvedminutedifference,
    DATEDIFF(minute, xxxxxx, CURRENT_TIMESTAMP) minutedifference
FROM 
WHERE uid = 3
ORDER BY autoid DESC