zknoll
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.ca lendar.sta rt_time,10 8) 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.
Convert(varchar(10),dbo.ca
which returns 08:00:00
I can't figure out if there is a style parameter that will give me what I need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
here format options in detail
http://doc.ddart.net/mssql/sql70/ca-co_1.htm
http://doc.ddart.net/mssql/sql70/ca-co_1.htm
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.
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.
ASKER
It would have easier to understand if you referenced by field in the solution.
but this may help
select convert(varchar(2), DATEPART(hour, getdate())) + convert(varchar(2), DATEPART(minute, getdate()))