Datetime HH:mm - zero missing

Hello
I would like to select hour and minutes from datetime. I would like to convert @MyDate so it becomes '09:05'
When I do following I get '9:5'  How do I convert to get zero as well?

DECLARE @MyDate datetime='2015-03-16 09:05:57.000'

select
CAST(DATEPART(HOUR, @MyDate) as varchar(50))+':'+CAST(DATEPART(MINUTE,@MyDate) as varchar(50))
johnson1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Lokesh B RDeveloperCommented:
Hi,

Use the following format

select Right(Convert(varchar(16), @MyDate, 121),5)

Open in new window


Example here

select Right(Convert(varchar(16), CAST('2015-03-16 09:02:37.723' AS datetime), 121),5)

select Right(Convert(varchar(16), CAST('2015-03-16 10:15:37.723' AS datetime), 121),5)

Open in new window

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
PortletPaulEE Topic AdvisorCommented:
select convert(varchar(5), @MyDate ,108)

for other date/time styles see: http://www.experts-exchange.com/Database/MS-SQL-Server/A_12315-SQL-Server-Date-Styles-formats-using-CONVERT.html

---
btw: if you have SQL 2012+

select format(@MyDate, 'hh:mm')
0
johnson1Author Commented:
Thank you:-)
0
PortletPaulEE Topic AdvisorCommented:
STYLE 108 is provides just the TIME (it ignores date)

so just take the first 5 chars of the time

DECLARE @MyDate datetime='2015-03-16 09:05:57.000'

SELECT CONVERT(varchar(5), @MyDate ,108)

Result:
09:05
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 2008

From novice to tech pro — start learning today.