Sunday, November 25, 2007

Get TIME alone from a given date

I see this to be very frequently asked question in dotnetspider.com! Solution to this is to make use of CONVERT function in SQL Server.

--Query will fetch the time portion alone
Select Convert(Varchar, Getdate(), 108)

--Query will fetch the date portion alone.
Select Convert(Varchar, Getdate(), 101)

The last parameter of Convert function is StyleId. Go through books online to know the complete list of parameters and its corresponding output format.

1 comment:

Madhivanan said...

Note that the result is actually of varchartype. Usually this type of formations should be done at front end application(If used)

http://sqlblogcasts.com/blogs/madhivanan