Date Storage Only in SQL Server 2005

How to avoid saving the temporary part datetimein SQL Server, i.e. if i have value 2011-01-01 00:00:00.000i want to save only 2011-01-01?

I want only part of the date to be stored.

+5
source share
7 answers

The DateTime data type ALWAYS stores the date and time. Thus, you can use CONVERT / CAST to get a specific format, or use the YEAR (), MONTH (), or DAY () methods to highlight date information based on your needs.

+7
source

- . , , , // .

+2

DATETIME . , 12:00 - .

: , , , . :)

+1

If you point the DateTime to Int and vice versa, you will get a DateTime from 00:00 as the time part. This way you can save all your dates as integers in the database.

+1
source

Or add the calculated column:

dateonly AS CONVERT(DATETIME, CONVERT(CHAR(8), date_with_time, 112), 112)

or truncate your date directly insert:

INSERT
INTO     mytable (dateonly)
VALUES   CONVERT(DATETIME, CONVERT(CHAR(8), GETDATE(), 112), 112)

creating CHECKa dateonly in the column to cause an error when someone tries to insert a non-truncated value:

CHECK (dateonly = CONVERT(DATETIME, CONVERT(CHAR(8), date_with_time, 112), 112))
+1
source

Just specify the date as an integer value yyyMMdd.

0
source

All Articles