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

x
?
Solved

sql zero before the minutes when < 10

Posted on 2006-05-04
3
Medium Priority
?
412 Views
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))
or
select cast(datepart(hour, GetDate()) as char(2))+':'+cast(datepart(minute, GetDate()) as char(2))

thx in advanced
0
Comment
Question by:justaphase
3 Comments
 
LVL 23

Accepted Solution

by:
adathelad earned 1600 total points
ID: 16604302
Hi,

You can just do:

SELECT CONVERT(VARCHAR(5), GETDATE(), 108)
0
 
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)


0
 
LVL 1

Author Comment

by:justaphase
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 :)
0

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