다음을 통해 공유


SQL Server Function to get the Hour of the Month

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

Technorati Tags:
SQL SERVER
,
SQL
,
YUKON
,
KATMAI
,
DateTime Function
,
User defined function

Comments