Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handy Date/Time UDFs

Status
Not open for further replies.

TJRTech

Programmer
Apr 8, 2002
411
US
Useful user-defined functions for massaging a date/time into TwelveAM and Midnight (actually 3ms before midnight):

Comments?

Code:
-- ** Midnight **
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Midnight]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[Midnight]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE FUNCTION [dbo].[Midnight] (@dt datetime)  
RETURNS datetime  AS  
BEGIN 
  declare @ret_dt datetime
  set @ret_dt = dateadd(ms, -3, dateadd(dd, 1, convert(datetime, convert(varchar(11), @dt))))
  return @ret_dt
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO



-- ** TwelveAM **
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TwelveAM]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[TwelveAM]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE FUNCTION [dbo].[TwelveAM] (@dt datetime)  
RETURNS datetime  AS  
BEGIN 
  declare @ret_dt datetime
  set @ret_dt = convert(datetime, convert(varchar(11), @dt))
  return @ret_dt
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top