How to add a 0 to the left in a datetime

1

I need to be able to add a 0 left at the time that sql only takes 3 digits ie from midnight to 9 in the morning for the information to be sorted!

Because if not the information I have is in the following order 0:00 1:00 10:00 am 11:00 a.m. 19:00 2:00 20:00 ....

And what I want is for the hours to be sorted ... My idea would be to transform the datetime into varchar to increment 0 and then the correct time, but I can not find the tools to get it done ... Can anyone help me?

select [Hora] 

case when len(cast([Hora] as varchar(4))) = 4 then '0' + cast([Hora] as varchar(2))


from [dbo].['Data16_Agosto-2016$']

(I'm sorry but I still do not know how to put the code with the code format here in overflow)

    
asked by anonymous 06.12.2017 / 17:40

1 answer

0

Use the format () method

Example:

select format(cast([NOME_DA_COLUNA] as DATETIME), "hh:mm") 
from [dbo].[NOME_DA_TABELA]
    
06.12.2017 / 19:27