1
I need to be able to add a 0 to the left in the hours when sql only takes 3 digits or it is from midnight to 9 am so that the information is ordered!
Because if not the information I have is in the following order 0:00 1:00 10:00 11:00 ..... 19:00 2:00 20:00....
And what I want is for the hours to be in order... My idea would be to turn the datetime into varchar to increase the 0 and only then the correct time, but I can not find tools to do it... Can someone 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 don’t know how to put the code with the format code here in the overflow)
Edit the question with your query that will help you.
– arllondias
@arllondias already put the code
– Ricarte
check in the language manual that you are using the LPAD function, it fills the fields on the left with the remaining fields you need, example LPAD([hour],2,0) It will complete with 0 until you reach 2 houses, if you already have 2 houses does nothing
– arllondias
@Ricarte How the column is declared
Hora
? What is the database manager? (mariaDB, Oracle Database, SQL Server etc.)– José Diz
@Josédiz SQL server
– Ricarte
@Josédiz SQL Sever.. The hour column is declared as datetime
– Ricarte
@Ricarte The column
Hora
is declared as datetime; ok. But what contains the tableData16_Agosto-2016$
? All the rows in this table are of the same date, as the name of the table suggests? It is to list the date and time or only the time?– José Diz
@Josédiz the table contains lots of data, the hour column only determines the time, I have another column 'Date' with the respective date! There are random records per hour, ie (ex) the range from 9:00 to 10:00 there may be 4 or 10, until there may not even be records at that time, it is random. but then, to group the information by, the hours are not sorted because after the 1:00 info it follows 11:00, 12:00 as if it were sorted alphabetically, (19:00, 2:00, 20:00) it is clear now?
– Ricarte