Wednesday, September 15, 2010

truncate time part from datetime in sqlserver 2005 or remove time part from datetime

Example:

My date looks like '2010-09-15 16:59:27.797'. If i want to remove time part and i want to display only date part we can use following code snippet.

Declare @test datetime


set @test=getdate()

select convert(varchar(10),@test,101)
 
After execution of above 3 statements output looks like;
-----------------
09/15/2010
 
Note: we may pass column name as argument insted of @test(variable).
 
 Ex: select convert(varchar(10),@column_name,101) from my_table
       my_table is table and @column_name is name of column that contains datetime values....

No comments:

Post a Comment