sql zero before the minutes when < 10

Posted on 2006-05-04
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


    You can just do:

    LVL 75

    Assisted Solution

    by:Aneesh Retnakaran
    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)

    LVL 1

    Author Comment

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Viewers will learn how the fundamental information of how to create a table.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now