Essential SQL Server T-SQL Date/Time User Functions
Jeff's SQL Server Weblog - Essential SQL Server Date and Time Functions
"I've posted some variations of these before, but here they all are in 1 easy package: The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any formatting considerations at all.
...
create function DateOnly(@DateTime DateTime)
-- Strips out the time portion of any dateTime value.
returns datetime
as
begin
return dateadd(dd,0, datediff(dd,0,@DateTime))
end
gocreate function Date(@Year int, @Month int, @Day int)
-- returns a datetime value for the specified year, month and day...
create function Time(@Hour int, @Minute int, @Second int)
-- Returns a datetime value for the specified time at the "base" date (1/1/1900)...
create function TimeOnly(@DateTime DateTime)
-- returns the only the time portion of a date, at the "base" date (1/1/1900)...
create function DateTime(@Year int, @Month int, @Day int, @Hour int, @Minute int, @Second int)
-- returns a dateTime value for the date and time specified....
Part II: Introducing TimeSpans to SQL Server
With those functions in place, we can add two more that will give us further flexibility when working with dates and times: The concept of a "TimeSpan", very similar to what is available in the .NET framework...."
I re-intent the "get Date only from DateTime" wheel to darn often. Hopefully posting a version here will help me find it in the future (assuming I remember I posted it... I'm getting old now... ;)
No comments:
Post a Comment