hh:mm in sql server (1 Viewer)

roosn

Registered User.
Local time
Today, 22:58
Joined
Jul 29, 2005
Messages
121
hi
i have a date field in sql server 2000, smalldatetime data type
what i cant get to is the hh:mm

for example

31/08/2005 12:01:00

using

CAST(DATEPART(hh, dbo.tbl_log.date) AS varchar) + ':' + CAST(DATEPART(mi, dbo.tbl_log.date) AS varchar)

i get 12:1

what i require is 12:01

thank you in advance
 

SQL_Hell

SQL Server DBA
Local time
Today, 22:58
Joined
Dec 4, 2003
Messages
1,360
Hi,

To deal with the lack of leading zeros, use a case statement...like so:

Code:
select 
case
when len(CAST(DATEPART(hh, getdate()) AS varchar)) =1 then '0' + CAST(DATEPART(hh, getdate()) AS varchar) 
else CAST(DATEPART(hh, getdate()) AS varchar)
end
+ ':' + 
case
when len(CAST(DATEPART(mi, getdate()) AS varchar)) =1 then '0' + CAST(DATEPART(mi, getdate()) AS varchar) 
else CAST(DATEPART(mi, getdate()) AS varchar)
end
 

roosn

Registered User.
Local time
Today, 22:58
Joined
Jul 29, 2005
Messages
121
tried and tested
thank you
 

Users who are viewing this thread

Top Bottom