This function returns an integer of the hour of the month passed as a variable.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[uf_GetHourOfMonth]')
AND type IN (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [util].[uf_GetHourOfMonth]
GO
CREATE FUNCTION [util].[uf_GetHourOfMonth](
@date DATETIME
)
RETURNS INTEGER
WITH EXECUTE AS CALLER
AS
/**********************************************************************************************************
* UDF Name:
* [util].[uf_GetHourOfMonth]
* Parameters:
* @date datetime - The date to convert
* Purpose: This function returns an integer of the hour of the month passed as a variable.
*
* Example:
select [util].[uf_GetHourOfMonth](GETDATE())
* Revision Date/Time:
* August 1, 2007
**********************************************************************************************************/
BEGIN
-- declare variables
DECLARE @result integer;
SET @result = (DAY(@DATE) - 1) * 24 + DATEPART(hh,@DATE);
-- return results.
RETURN @result;
END;
SELECT [util].[uf_GetHourOfMonth](GETDATE());