I have this code which is not working, the code supposed to format date and make it display 2days ago , 2years ago or 1hr ago
Create FUNCTION GetTimeFromCurrentDate
(
@TempDate DATETIME
)
RETURNS nvarchar(200)
AS
BEGIN
DECLARE @TimeFormat NVARCHAR(200)
DECLARE @Days INT
DECLARE @Hours INT
DECLARE @Minute INT
SET @Days=DATEDIFF(dd,@TempDate,getdate())
SET @Hours=DATEDIFF(hh,@TempDate,getdate())
SET @Minute=DATEDIFF(mi,@TempDate,getdate())
SET @Minute=@Minute-@Hours*60
if @Days=0 and @Hours=0 and @Minute>0
BEGIN
set @TimeFormat=CONVERT(VARCHAR,@Minute) + 'minute ago'
END
else if @Days=0 and @Hours>0 and @Minute>0
BEGIN
set @TimeFormat=CONVERT(VARCHAR,@Hours)+' hours, '+CONVERT(VARCHAR,@Minute)+ 'minute ago'
END
else if @Days>0 and @Hours=0 and @Minute>0
BEGIN
set @TimeFormat=CONVERT(VARCHAR,@Days)+' days, '+CONVERT(VARCHAR,@Hours)+' hours, '+CONVERT(VARCHAR,@Minute)+ 'minute ago'
END
RETURN @TimeFormat;
END
And it into the your SQL query as:
SELECT t.Id as Id,t.Name,t.ImageName,v.FriendId,s.ImageName1,s.MyId,s.SendDate,s.Message,s.SendDate,s.ID as ScrapId, dbo.GetTimeFromCurrentDate(s.SendDate) as timeFromCurrentDate
FROM [User] as t, USERPOST as s, Friends2 as v WHERE s.MyId=t.ID AND v.MyId = @Id ORDER BY Id DESC