Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get only Time from GetDate(). Format is hh:mm PM/AM.

Finally my requirement is to get Random Time between 10.00 AM to 8.30 PM.
Posted
Updated 9-Sep-11 3:20am
v2

you can cast the date to a time. For example:
SQL
select cast(getdate() as time)
 
Share this answer
 
Comments
LebneizTech 9-Sep-11 9:28am    
not working
Wendelius 9-Sep-11 9:37am    
Do you get an error or why it isn't working? Also what version of SQL Server are you using?
Corporal Agarn 9-Sep-11 12:28pm    
Which version of SQL do you have. TIME is a new data type.
Wendelius 9-Sep-11 14:22pm    
2008 R2, time data type was introduced in version 2008
LebneizTech 12-Sep-11 6:30am    
Sir, I am using SQL 2000/2005 that's why it shows error
Use
CONVERT( VARCHAR(8), Getdate(), 108)


Lookup CAST and CONVERT
 
Share this answer
 
You can use TimeOfDay property to get the time of a special date:


C#
 DateTime Date = new DateTime(Your datetime);
String Time = Date.TimeOfDay.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900