Compartir a través de


SQL Server Function to get the Hour of the Year

This function returns an integer of the hour of the year passed as a variable.

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

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

Comments