T-SQL自定义函数返回前一天或后一天日期
下面两个自定义函数,一个是获取指定的前一天的日期,另外一个是获取指定日期的后一天日期。开发企业内网软件常用,为了更好的的规范和代码维护,Insus.NET常会把一些重复使用的代码,重构为函数。
返回前一天:
udf_Yesterday
-- =============================================
-- Author: Insus.NET
-- Create date: 2012-03-22
-- Description: 指定日期,返回前一天日期
-- =============================================
CREATE FUNCTION [dbo].[udf_Yesterday](@datetime DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(DAY,-1,@datetime)
END
GO
-- Author: Insus.NET
-- Create date: 2012-03-22
-- Description: 指定日期,返回前一天日期
-- =============================================
CREATE FUNCTION [dbo].[udf_Yesterday](@datetime DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(DAY,-1,@datetime)
END
GO
返回后一天:
udf_Tomorrow
-- =============================================
-- Author: Insus.NET
-- Create date: 2012-03-22
-- Description: 指定日期,返回后一天日期
-- =============================================
CREATE FUNCTION [dbo].[udf_Tomorrow](@datetime DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(DAY,1,@datetime)
END
GO
-- Author: Insus.NET
-- Create date: 2012-03-22
-- Description: 指定日期,返回后一天日期
-- =============================================
CREATE FUNCTION [dbo].[udf_Tomorrow](@datetime DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(DAY,1,@datetime)
END
GO
Demo:
SELECT [dbo].[udf_Yesterday](CURRENT_TIMESTAMP) AS [Yesterday]
SELECT [dbo].[udf_Tomorrow](CURRENT_TIMESTAMP) AS [Tomorrow]
SELECT [dbo].[udf_Tomorrow](CURRENT_TIMESTAMP) AS [Tomorrow]
Result: