sql zero before the minutes when < 10

Hi experts,

I have a query that returns the hours and minutes.
But the problem is: if the hour is 12:02, returns 12:2 and i need the minutes like this "02", not like this "2". I need the zero to be printed.

Where's the query i use:

select cast(datepart(hour, GetDate()) as char(2))+':'+cast(datepart(mi, GetDate()) as char(2))
or
select cast(datepart(hour, GetDate()) as char(2))+':'+cast(datepart(minute, GetDate()) as char(2))

thx in advanced
LVL 1
justaphaseAsked:
Who is Participating?
 
adatheladConnect With a Mentor Commented:
Hi,

You can just do:

SELECT CONVERT(VARCHAR(5), GETDATE(), 108)
0
 
Aneesh RetnakaranConnect With a Mentor Database AdministratorCommented:
If you still wish to use your query, modify it as


select RIGHT('0'+cast(datepart(hour, GetDate()) as varchar(2)),2)+':'+ RIGHT('0'+cast(datepart(mi, GetDate()) as varchar(2)),2)

select RIGHT('0'+cast(datepart(hour, GetDate()) as varchar(2)),2)+':'+RIGHT('0'+cast(datepart(minute, GetDate()) as varchar(2)),2)


0
 
justaphaseAuthor Commented:
Very good!! :D

I gave more points to the first(adathelad) because was the first and more logical, and the second one(aneeshattingal) is very very interesting and gives ideas, thx both :)
0
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.

All Courses

From novice to tech pro — start learning today.