Time format hhmm to hh:mm sql server 2005

2019-07-30 02:05发布

问题:

I want to find the time difference so i am trying to use DATEDIFF option. But the problem is i am having the time values in hhmm format(eg: 1715). So it showing out of range error. so i want to convert it to hh:mm format. How to convert hhmm to hh:mm format?

回答1:

Have you tried something like

DECLARE @TimeVal VARCHAR(4)
SELECT  @TimeVal = '1715'

select CONVERT(DATETIME,LEFT(@TimeVal,2) + ':' + RIGHT(@TimeVal,2),8)

Output

1900-01-01 17:15:00.000