Timeformat: hhmmss

Hi I need to have the time format in hhmmss

SELECT CONVERT(VARCHAR(8),GETDATE(),108)

15:31:42
I need it to be 153142
is there the way to do it?
Thanks

REPLACE(CONVERT(varchar(8), GETDATE(), 108), ':', '')
2 Likes

thank you

Hi

Another way

Direct way

Format ( getdate() , 'hhmmss' )

Hope this helps

:+1::+1:

You'd need to use the format string 'HHmmss' to get the 24 hour version.
Also, fyi, Format() will take about 10x longer, which may not matter on small data sets, but it can add up in a hurry.

use StackOverflow2013;
set statistics time on;
select max( replace(convert(varchar(8), creationdate, 108), ':', '') ) from votes;
 SQL Server Execution Times:
   CPU time = 33044 ms,  elapsed time = 4542 ms.
select max( format(creationdate, 'HHmmss')							 ) from votes;
 SQL Server Execution Times:
   CPU time = 588656 ms,  elapsed time = 78589 ms.

hi

hope this helps

i was thinking of another approach .. which could possibly be faster

please have a look

select   RIGHT('00'+cast(datepart(hh,Dt) as varchar) ,2) 
	  +  RIGHT('00'+cast(datepart(mi,Dt) as varchar) ,2)
      +  RIGHT('00'+cast(datepart(ss,Dt) as varchar) ,2)
from #data