Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting varchar(6) in HHmmss format to sql time format

How is it possible to convert something stored in DB as varchar(6) (e.g., 180000 which is actually in a format of HHmmss) to time, I tried the following but wont work

select convert(time,'180000')
select cast('180000' as time)
like image 738
Vishnu Babu Avatar asked Oct 18 '25 20:10

Vishnu Babu


2 Answers

One more approach

DECLARE @t VARCHAR(6)='180000';

SELECT CAST(STUFF(STUFF(@t,5,0,':'),3,0,':') AS time)

Thanks to @SebtHU, who pointed out, that this would not work with leading zeros in times such as 09:15:00 or 00:45:00. You can use this instead:

SELECT CAST(STUFF(STUFF(RIGHT(CONCAT('000000',@t),6),5,0,':'),3,0,':') AS time);
like image 71
Shnugo Avatar answered Oct 21 '25 11:10

Shnugo


try this :

select cast((substring('180000',0,3)+':'+substring('180000',3,2)+':'+substring('180000',5,2)) as time)
like image 31
Ranjana Ghimire Avatar answered Oct 21 '25 11:10

Ranjana Ghimire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!