SELECT dbo.fnAddDateAndTimeForNewDateTime('03/08/2010 10:30:00', '03/15/2010 10:43:56')
-- return 2010-03-08 10:43:56.000
CREATE FUNCTION [dbo].[fnAddDateAndTimeForNewDateTime]
(
	@dateTime1	DATETIME,
	@dateTime2	DATETIME
)
RETURNS DATETIME
AS
BEGIN
	DECLARE	@dateOnly		DATETIME,
			@timeOnly		DATETIME,
			@newDateTime	DATETIME;

	IF(ISDATE(@dateTime1) = 1 AND ISDATE(@dateTime2) = 1)
	BEGIN
		SET @dateOnly = CAST(@dateTime1 AS DATE);
		SET @timeOnly = CAST(@dateTime2 AS TIME(0));
		SET @newDateTime = @dateOnly + @timeOnly;
	END;
	RETURN @newDateTime;
END;
Last modified: May 21, 2020

Author

Comments

Write a Reply or Comment