Getting a Date Only Function in Sql Server

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 297
Write below user defined function,which will return varchar as:-
create function [dbo].[fn_Getdateonly] (@pinputdate datetime)
returns varchar(10)
begin
return cast(year(@pinputdate) as varchar(4)) + '/' +
cast(month(@pinputdate) as varchar(2)) + '/' +
cast(day(@pinputdate) as varchar(2));
end


To use above function,we will write:-
select dbo.fn_Getdateonly(Getdate());

Output:-2014/10/20
select dbo.fn_Getdateonly('2014-10-20');

Output:-2014/10/20
select dbo.fn_Getdateonly('10/20/2014');

Output:-2014/10/20

Comments or Responses

Login to post response