SQL Case Statement - Time Format issue

I have the following issue with some data in a table
TRANS_TIME = HHMMSS (Example 10:00AM = 100000)

We have the command below to convert the time because we have sometimes in TRANS_TIME a value of 09:00AM, which is stored as 90000. So we use the statement below :-

CASE WHEN TRANS_TIME >= 100000 THEN CAST(TRANS_TIME AS char(6)) ELSE '0'+ CAST(KAS_ZEIT AS char(5)) END

Problem is we have TRAN_TIME values of 300 for (00:03:00) for example causing issue and wanted to to know the best way to deal will all interpretations of TRANS_TIME up until the last second (00:00:01) which is stored in TRANS_TIME as 1.
missing_dllAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try replace

CASE WHEN TRANS_TIME >= 100000 THEN CAST(TRANS_TIME AS char(6)) ELSE '0'+ CAST(KAS_ZEIT AS char(5)) END

to

right('00000'+ KAS_ZEIT, 6)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Deepak ChauhanSQL Server DBACommented:
Try this , I think this is what you are looking for.

select  STUFF(STUFF(REPLICATE('0',6-LEN(TRANS_TIME)) + 
convert(VARCHAR(6),TRANS_TIME),3,0,':'),6,0,':') as TRANS_TIMES

From Table

Open in new window

0
HuaMin ChenProblem resolverCommented:
Try
-- Convert one number into one time
declare
  @int int,
  @str varchar(10),
  @str2 varchar(10),
  @len int,
  @tm datetime
  
set @str='000000'
set @int=300
set @str2=rtrim(ltrim(cast(@int as varchar)))
set @len=len(@str2)
set @str2=substring(@str,1,6-@len)+@str2
set @str2=substring(@str2,1,2)+':'+substring(@str2,3,2)+':'+substring(@str2,5,2)
select CONVERT(datetime,@str2,114)

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
No need for CASE. Try this:
SELECT STUFF(STUFF(RIGHT('000000' + CAST(TRANS_TIME AS VARCHAR),6),3,0,':'),6,0,':') 

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.