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 2005 Forums
 Other SQL Server Topics (2005)
 have zero in left field

Author  Topic 

wided
Posting Yak Master

218 Posts

Posted - 2010-04-28 : 13:14:52

field in database datetime

field = var1
data var1 = exemple : '12/04/2010 08:30:00'
table = table1

select datepart(hh, var1) from table1
then result is int
i want to have the result like this
08 or 008 or 0008 .....

i need this result

thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-28 : 13:26:11
You'll need to convert the result to varchar using CONVERT function and then use RIGHT function to add the zeros in.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-29 : 04:11:15
select right('00'+convert(varchar(2),datepart(hh, var1)),2) from table1
gives '08'.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-29 : 06:24:56
or

select convert(varchar(2),var1,108) from table1

Madhivanan

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

- Advertisement -