intellectually constipated

patrick gallucci's technology brain drain

SQL Server Function to get the Hour of the Month

SQL Server Function to get the Hour of the Month

Rate This
  • Comments 1

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;
GO
SELECT [util].[uf_GetHourOfMonth](GETDATE());
GO
Comments
Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post