tags:

views:

1114

answers:

5

Scenario: A stored procedure receives from code a DateTime with, let's say DateTime.Now value, as a datetime parameter. The stored procedure needs to store only the date part of the datetime on the row, but preserving all date related arithmetics for, to say, do searches over time intervals and doing reports based on dates.

I know there is a couple of ways, but what is the better having in mind performance and wasted space?

A: 

store the date with time = midnight

EDIT: i was assuming MS SQL Server

Jared
It is, it's tagged tsql. thanks for the answer.
David Lay
A: 

Essentially you're only going to store the Date part of your DateTime object. This means regardless of how you wish to handle querying the data the Date returned will always be set to 00:00:00.

Time related functions are useless in this scenario (even though your original DateTime object uses them) as your database drops this info.

Date related arithmetics will still apply though you will have to assume a time of midnight for each date returned from the database.

Colorado
+3  A: 

Business Logic should be handled outside of the proc. The procs jobs should be to save the data passed to it. If the requirment is to only store Date and not time, then the BL/DL should pass in DateTime.Now**.Date** (or the equiv...basically the Date part of your DateTime object).

If you can't control the code for some reason, there's always convert(varchar(10), @YOURDATETIME, 101)

Tom Carr
I can further comment, that it worked on my production code. On VbNet, I managed to modify all setters with DateTime type to receive only the date by saying "_variable = value.Date" and from then on, it propagated to de database back and forth gracefully. Thanks!
David Lay
A: 

SQL Server 2008 has a date only type (DATE) that does not store the time. Consider upgrading.

http://www.sqlteam.com/article/using-the-date-data-type-in-sql-server-2008

TonyOssa
A: 

If you're working on Oracle, inside your stored procedure use the TRUNC function on the datetime. This will return ONLY the date portion.

Mike McAllister