Link to home
Start Free TrialLog in
Avatar of zknoll
zknollFlag for United States of America

asked on

Format output of time from a datetime field

I'm extracting data from sql server and need to output the data in a specific order and format.  The field I'm having trouble with is defined in sql server as a datetime field (8).  My extract requires the time to be formatted as a numeric 4 chars, as in military time with leading zeros, so 8:00 should appear as 0800   Here is my current statement:

Convert(varchar(10),dbo.calendar.start_time,108) as BegTime
which returns 08:00:00

I can't figure out if there is a style parameter that will give me what I need.
Avatar of Faizan Sarwar
Faizan Sarwar
Flag of United Kingdom of Great Britain and Northern Ireland image

i am not sure aobut style param
but this may help
select convert(varchar(2), DATEPART(hour, getdate())) + convert(varchar(2), DATEPART(minute, getdate()))
ASKER CERTIFIED SOLUTION
Avatar of dhanushkad
dhanushkad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zknoll

ASKER

convert(varchar(2), DATEPART(hour, getdate())) + convert(varchar(2), DATEPART(minute, getdate()))

This works for a time like 10:30.  The output is 1030 but if the time is 8:00:00 the output is 80 so I'm missing the leading 0 before the 8 and the second 0 for the minute.
Avatar of zknoll

ASKER

It would have easier to understand if you referenced by field in the solution.