patrick gallucci's sql server brain drain
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
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
About Patrick Gallucci
breathing air