View Thread
 Print Thread
Function to DATEFIFF() but in words, EG: 3 days, 2 hours
admin
Function to DATEFIFF() but in words, EG: 3 days, 2 hours


SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[udf_datediffToWords]
(
    @d1 DATETIME,
    @d2 DATETIME
)
RETURNS VARCHAR(255)
AS
BEGIN
    DECLARE @minutes INT, @word VARCHAR(255)
    SET @minutes = ABS(DATEDIFF(MINUTE, @d1, @d2))
    IF @minutes = 0
        SET @word = '0 minutes.'
    ELSE
    BEGIN
        SET @word = ''
        IF @minutes >= (24*60)
            SET @word = @word 
            + RTRIM(@minutes/(24*60))+' day(s), '
        SET @minutes = @minutes % (24*60)
        IF @minutes >= 60
            SET @word = @word 
            + RTRIM(@minutes/60)+' hour(s), '
        SET @minutes = @minutes % 60
        SET @word = @word + RTRIM(@minutes)+' minute(s).'
    END
    RETURN @word
END
GO

Jordon Pilling | Heavencore Administrator
 
Jump to Forum