Best way to store only date in date field?

Scenario: A stored procedure receives from a DateTime code with, say, the value DateTime.Now, as a datetime parameter. A stored procedure should only store a portion of the date and time date in a string, but retain all the arithmetic associated with the date in order, say, to search by time intervals and make reports based on dates.

I know there are several ways, but which is better, bearing in mind the performance and wasted space?

+3
source share
5 answers

Business logic must be handled outside of proc. The tasks of procs should be to store the data transferred to it. If the request should only store the date, not the time, then BL / DL should pass in DateTime.Now **. Date ** (or equiv ... basically, in the Date part of your DateTime object).

If for some reason you cannot control the code, always convert (varchar (10), @YOURDATETIME, 101)

+4
source

save date with time = midnight

EDIT: I was taking MS SQL Server

0
source

, Date DateTime. , , , ​​ 00:00:00.

, , ( DateTime ), .

Date-related arithmetic will continue to apply, although you will have to take the midnight time for each date returned from the database.

0
source

SQL Server 2008 only has a date type (DATE) that does not save time. Consider updating.

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

0
source

If you are working on Oracle, use the TRUNC function in datetime inside your stored procedure. This will return ONLY a portion of the date.

0
source

Source: https://habr.com/ru/post/1697022/


All Articles