intellectually constipated

patrick gallucci's sql server brain drain

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
Published Tuesday, September 25, 2007 4:32 PM by Patrick Gallucci

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Patrick Gallucci

breathing air

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker