查询需要小时和秒

时间:2018-08-18 08:53:45

标签: sql sql-server-2008

我正在计算2次之间的时差,我想打印小时的分钟和秒。谁能告诉我怎么做。

我的查询

SELECT 
    CONVERT(VARCHAR(8), DATEADD(ms, DATEDIFF(ms, CONVERT(VARCHAR(8), GETDATE(), 114), CONVERT(VARCHAR(8), VCTime, 114)), 0), 114) AS TImeDifference 
FROM
    Test

输出:

TimeDifference  
---------------
10:51:37    
20:51:37   
21:51:37   
22:21:37   
08:51:37   
00:51:37   

预期产量

TimeDifference  
---------------
10h:51m:37s    
20h:51m:37s   
21h:51m:37s   
22h:21m:37s   
08h:51m:37s   
00h:51m:37s   

4 个答案:

答案 0 :(得分:1)

一种方法是将+的子查询和串联运算符2008DATEPART函数一起使用,如下所示:

SELECT (
    CAST(DATEPART(HOUR,(TImeDifference)) AS VARCHAR) + 'h:' +
    CAST(DATEPART(MINUTE,(TImeDifference)) AS VARCHAR) + 'm:' + 
    CAST(DATEPART(SECOND,(TImeDifference)) AS VARCHAR) + 's')
FROM(
SELECT 
    CONVERT(varchar(8), DATEADD(ms, DATEDIFF(ms, convert(varchar(8),getdate(),114), 
    convert(varchar(8),VCTime,114)), 0), 114) as TImeDifference 
    FROM test
) t

是的,我意识到concat是在2012中引入的,因此我们可以改用+

答案 1 :(得分:0)

您可以按照以下方式

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have a notification from : ', user_id);


if (!change.after.val()) {
    return console.log("A Notification has been deleted from the database", notification_id);
}

const fromUser = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

return fromUser.then(fromUserResult => {

    const from_user_id = fromUserResult.val().From;

    console.log('You have new notification from  : ', from_user_id);

    const userQuery = admin.database().ref(`UserData/${from_user_id}/Name`).once('value');
    return userQuery.then(userResult => {
        const userName = userResult.val();

        const deviceToken = admin.database().ref(`/UserData/${user_id}/TokenID`).once('value');
        return deviceToken.then(result => {

            const token_id = result.val();

            const payload = {
                notification: {
                    title: "New Message",
                    body: `${userName} has sent you a message`,
                    icon: "default",
                    click_action: "com.appmaster.akash.messageplus_TARGET_NOTIFICATION"
                },
                data: {
                    from_user_id: from_user_id
                }
            };

            return admin.messaging().sendToDevice(token_id, payload).then(response => {

                return console.log('This was the notification Feature');

            });

        });

    });
});
});

这将返回 1:05:57:00

答案 2 :(得分:0)

首先,您不应该转换为字符串来获得差异。我认为应该没问题:

SELECT CONVERT(VARCHAR(8),
               DATEDIFF(ms, CAST(GETDATE() as TIME), CAST(VCTime as TIME)),
               114
              ) as TImeDifference 
FROM Test;

然后您要添加“ h”,“ m”和“ s”。您可以使用STUFF()函数。但是让我使用APPLY来执行此操作,以使代码看起来不太混乱:

SELECT ( STUFF(STUFF(TimeDifference_str, 6, 0, 'm'), 3, 0, 'h') + 's' ) as TimeDifference_hms
FROM test t CROSS APPLY
     (VALUES (CONVERT(VARCHAR(8),
                      DATEDIFF(ms, CAST(GETDATE() as TIME), CAST(VCTime as TIME)),
                      114
                     )
             )
     ) v(TimeDifference_str)

答案 3 :(得分:0)

尝试一下:

select cast(date_diff / 3600 as varchar(4)) + 'h:' +  
       cast((date_diff % 3600) / 60 as varchar(4)) + 'm:' +
       cast(date_diff % 60 as varchar(4)) + 's'
from (
    select datediff(second, getdate(), VCTime) date_diff from my_table
) a