将 smallint 转换为时间 [英] Convert smallint to time

查看:36
本文介绍了将 smallint 转换为时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 smallint 转换为 time 格式.

I am trying to convert a smallint to a time format.

我使用的 SQL Server 2005 不支持 time 数据类型.

I am using SQL Server 2005 which doesn't support the time datatype.

我将时间存储为 1400,这是一个 smallint.检索时,我希望将其转换为时间格式.如14:00

I am storing the time as 1400 which is a smallint. When retrieving I want it to be converted to a time format. Such as 14:00

关于此事的任何想法或指导.如果有更简单的方法来做到这一点,或者我尝试的方式是否可行?

Any ideas or guidance on the matter. If there is an easier way to do it or if the way I am trying is possible?

推荐答案

您可以使用 varchar 获得结果:

You can get a result as varchar by using this:

SELECT 
    RIGHT('0' + CONVERT(varchar(10), yourTime / 100), 2) + ':' +  
    RIGHT('0' + CONVERT(varchar(10), yourTime % 100), 2) As timeString
FROM
    yourTable

你也可以得到这样的 DATETIME 格式的结果:

You can also have a result in DATETIME format like this:

SELECT 
    CONVERT(datetime, CONVERT(varchar(10), yourTime / 100)+ ':' + CONVERT(varchar(10), yourTime % 100))
FROM
    yourTable

<小时>

在 SQL Server 2012+ 中,您可以得到时间格式的结果:


In SQL Server 2012+ you can have a result in time format:

SELECT 
    TIMEFROMPARTS(yourTime / 100, yourTime % 100, 0, 0, 0) As timeFormat
FROM 
    yourTable

这篇关于将 smallint 转换为时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆