Adds TIME portion of a DT to the DATE of another

,

UDF that returns a DATETIME which is the concatination of the TIME portion of one DATETIME and the DATE portion of another.
EXAMPLES:
DECLARE @Date DATETIME, @Time DATETIME
SET @Date = '7/1/03 16:00'
SET @Time = '5/16/1999 9:30 AM'
PRINT dbo.FN_AddDateTime(@Date,@Time )
RETURNS: Jul  1 2003  9:30AM

PRINT dbo.FN_AddDateTime(@Date,0)
RETURNS: Jul  1 2003 12:00AM

CREATE FUNCTION dbo.FN_AddDateTime (@Date DATETIME, @Time DATETIME)  
RETURNS DATETIME AS  
BEGIN 

DECLARE @BTime BINARY(8)
DECLARE @BDay BINARY(8)
DECLARE @BSum BINARY(8)

SELECT @BTime = CONVERT(BINARY(8),@Time)
SELECT @BDay = CONVERT(BINARY(8),@Date)
SELECT @BSum = CONVERT(BINARY(8),STUFF(CONVERT(VARCHAR(8),@BDay),5,8,RIGHT(CONVERT(VARCHAR(8),@BTime),4)))

RETURN CONVERT(DATETIME,@BSum)
END

Rate

Share

Share

Rate