共用方式為


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

 

Technorati Tags:
SQL
,
SQL SERVER
,
Microsoft SQL Server
,
Function
,
Data Warehouse
,
Data Mining
,
Date Function
,
DateTime Function

Comments