Answered by:
Get DATE from DATETIME field

Question
-
how can i get the date only from the datetime field
datetimefield
2010-07-14 12:48:30.000
2010-08-11 18:50:30.000
2010-07-07 19:24:30.000
2010-08-05 17:27:30.000Friday, July 29, 2011 9:24 AM
Answers
-
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', datetimefield), '19000101')
Best regards- Marked as answer by Sam233 Friday, July 29, 2011 9:46 AM
Friday, July 29, 2011 9:34 AM
All replies
-
If you use 2008+
CONVERT(DATE, datetimefield)
Friday, July 29, 2011 9:25 AM -
select convert(char(10),datetimefield,121)
Best regardsFriday, July 29, 2011 9:26 AM -
Hi Sam
You can convert your datetime as the varchar and then extract the first 10 charecters from it.
see the below code
select left(convert(varchar(30),datetimefield,20),10)
Nothing is Permanent... even Knowledge....
My BlogFriday, July 29, 2011 9:27 AM -
can this be kept in datetime datatype because i need use the date within SSRS 2005 on a parameter selectionFriday, July 29, 2011 9:30 AM
-
Please be careful when cast to string.
Here a excellent article from Tibor about it:
- Proposed as answer by Naomi N Friday, July 29, 2011 9:54 PM
Friday, July 29, 2011 9:33 AM -
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', datetimefield), '19000101')
Best regards- Marked as answer by Sam233 Friday, July 29, 2011 9:46 AM
Friday, July 29, 2011 9:34 AM -
if you are using SQL Server 2005, only date or only time data types are not there. even if you extract the date some how and store it in the datetime filed it will be converted again as DD-MMM-YYYY HH:MM:SS format....
as per your requirement if your SSRS report have to fetch only the date from the datetime field. Then you can use the simple convert statement from SQL Server which will give only the date from the datetime, (convert it as a varchar)...
or if you want to store the extracted date in a date type filed only the date should be stored. You have to create a custom type.
Please follow the blog post from Jeff for this custom type creation
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
Nothing is Permanent... even Knowledge....
My BlogFriday, July 29, 2011 9:49 AM