Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing the date Only in SQL Server 2005

How do I avoid storing the time portion of a datetime in SQL Server, i.e. if I have a value of 2011-01-01 00:00:00.000 I want to store only 2011-01-01?

I want to ensure that only the date portion is stored.

like image 870
MAC Avatar asked Aug 24 '09 06:08

MAC


People also ask

How do I select only the date in SQL?

Use CASTSELECT CAST(getdate() AS date);

How do I extract just the day from a date in SQL?

If you want to get a day from a date in a table, use the SQL Server DAY() function. This function takes only one argument – the date. This can be a date or date and time data type.


2 Answers

SQL Server Date Formats.

like image 25
Derek Swingley Avatar answered Oct 29 '22 18:10

Derek Swingley


The DateTime data type ALWAYS stores the date AND time. So you are left with using CONVERT/CAST to obtain a particular format, or use the YEAR(), MONTH() or DAY() methods to isolate date details depending on your need.

like image 69
OMG Ponies Avatar answered Oct 29 '22 20:10

OMG Ponies