Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

store the date with time = midnight

EDIT: i was assuming MS SQL Server

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top