Add leading zeroes to time

SELECT RIGHT(CONCAT('000000',13000),6)

When a time is saved as integer the leading zeroes will not be saved. (E.g. 1:30 am => 013000 => 13000). To add the leading zeroes we can use the above snippet. Now we can cast it to our datatype time.

-- for postgres
SELECT RIGHT(CONCAT('000000',13000),6)::time
-- MSSQL needs the additional colon
SELECT CAST(STUFF(STUFF(RIGHT(CONCAT('000000', 13000) ,6),3,0,':'),6,0,':') as time )

Leave a Comment

Your email address will not be published. Required fields are marked *

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

hungsblog | Nguyen Hung Manh | Dresden
Scroll to Top