Beginning of Day User Function in SQL

19. February 2009 00:04

This simple user function will return the beginning of the day given a datetime in SQL. 

ALTER FUNCTION [dbo].[fnBeginningOfDay](@date DATETIME)

RETURNS DATETIME

AS

BEGIN

DECLARE @finalDate DATETIME

SET @finalDate = (SELECT CAST(DATEPART(mm,@date) AS VARCHAR(5)) + '/' + CAST(DATEPART(dd,@date) AS VARCHAR(5)) + '/' + CAST(DATEPART(yyyy,@date) AS VARCHAR(10)))

RETURN @finalDate

END

You can test it with the following:

SELECT dbo.fnBeginningOfDay('01/02/2009 06:30:00 AM')

SELECT dbo.fnBeginningOfDay(getDate())

Tags:

Microsoft SQL

blog comments powered by Disqus



My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts