intellectually constipated

patrick gallucci's sql server brain drain

SQL Server Function to return half year number of days.

This function returns an integer of the number of days in the half year.

 

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[uf_GetHalfYearDays]') 
AND type IN (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [util].[uf_GetHalfYearDays]
GO
CREATE FUNCTION [util].[uf_GetHalfYearDays](
 @date DATETIME
)
RETURNS INTEGER
WITH EXECUTE AS CALLER
AS
/**********************************************************************************************************
* UDF Name: 
* [util].[uf_GetHalfYearDays]
* Parameters: 
* @date datetime - The date to convert
* Purpose: This function returns an integer of the number of days in the half year.
*
* Example:
 select [util].[uf_GetHalfYearDays](GETDATE())
* 
* Revision Date/Time:
* August 1, 2007
*
**********************************************************************************************************/
BEGIN
 -- declare variables
 DECLARE @result integer;
 DECLARE @month integer;
 DECLARE @halfdate DATETIME;
 
 -- determine half year date
 SET @halfdate = CAST(CAST(((((MONTH(@date) - 1) / 6) * 6) + 1) AS VARCHAR) + '-1-' + CAST(YEAR(@date) AS VARCHAR) AS DATETIME);
 -- calculate days 
 SET @result = DATEDIFF(DAY,@halfdate,@date);
 -- return results.
 RETURN @result;
 
END;
GO
SELECT [util].[uf_GetHalfYearDays](GETDATE());
GO

 

Published Monday, September 24, 2007 10:29 AM 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