sqlsql-servert-sqldatetimesql-convert

TSQL Convert VARCHAR of HHMMSS to TIME


I have a VARCHAR field that has sample values as listed below and I would like to convert it to HH:MM:SS. Suggestions?

151200
085800
080100
210100
083300

Solution

  • On Sql-Server try this:

    declare @t varchar(10) 
    set @t='151200'
    
    select left(@t,2) + ':' + substring(@t,3,2) + ':' + right(@t,2)