[Webinar] Learn how to a build a cloud-first strategyRegister Now


sql zero before the minutes when < 10

Posted on 2006-05-04
Medium Priority
Last Modified: 2012-06-27
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))
select cast(datepart(hour, GetDate()) as char(2))+':'+cast(datepart(minute, GetDate()) as char(2))

thx in advanced
Question by:justaphase
LVL 23

Accepted Solution

adathelad earned 1600 total points
ID: 16604302

You can just do:

LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 400 total points
ID: 16604417
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)


Author Comment

ID: 16604457
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 :)

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question