Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Date and Time Conversion

Author  Topic 

suchiate
Starting Member

33 Posts

Posted - 2006-09-24 : 12:13:46
Hey guys.. I am having difficulties in returning the time in the format that I want...

i want to return the hours and minutes in 2 values such that:

3AM = 03:00

but it will only return 3:0 if i use the datepart(hh...) and datepart(mi...)... how can we always make them appear as two values?

help me out pls.. thanks!

SamC
White Water Yakist

3467 Posts

Posted - 2006-09-24 : 12:57:09
Post your code? We'll correct it for you.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2006-09-24 : 12:59:04
I think you are asking for a method which will include leading zeros...

RIGHT('0' + DATEPART(...), 2)

Sam
Go to Top of Page

suchiate
Starting Member

33 Posts

Posted - 2006-09-24 : 13:04:30
Select convert(varchar(2),datepart(hh,getdate())) + convert(varchar(2),datepart(mi,getdate())) as time
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-09-24 : 13:25:13
As a rule of thumb, you should never format your data in T-SQL. Return the raw data to your client/reporting tool, and let the presentation layer format it any way that it wants. T-SQL is not designed for formatting or outputing data. You can do it with some work, but the result is that everything must be converted to varchar's and now you are no longer returning data in the proper type.

- Jeff
Go to Top of Page

suchiate
Starting Member

33 Posts

Posted - 2006-09-24 : 13:28:34
yes i converted into varchar but i am still not getting the leading zeros...

i have tried SamC's method but the leading zero still dont work..
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-09-24 : 14:06:48
[code]select [HH:MM] = convert(varchar(5),getdate(),108)[/code]Results:
[code]
HH:MM
-----
14:04

(1 row(s) affected)[/code]

CODO ERGO SUM
Go to Top of Page

suchiate
Starting Member

33 Posts

Posted - 2006-09-24 : 14:07:54
guys thanks for the help.. i managed to figure out already..
thanks alot
below is the code...
RIGHT('0' + (Convert(varchar(2),DATEPART(...)), 2)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-24 : 20:45:04
Where do you want to show data?
If you use front end application, as suggested, do the formation there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -