Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

DATETIME to DATE...to cast, or not to cast?

$
0
0
Below is a code sample to help ask this question.  There is a DATETIME value that is passed in as a parameter from a report which needs to be put into a DATE datatype.  Both @TestDate1 and @TestDate2 return the correct answer.  However, is @TestDate1 the "correct" approach or is @TestDate2?  @TestDate2 does an explicit CAST whereas there must be an implicit CAST happening for @TestDate1 (this is an assumption).  If one approach is better than the other, please explainwhy.
--simulates data that is passed in as a parameter from report
declare @SomeDate datetime = '2011-11-20 14:35:29.123';

select @SomeDate as SomeDate;


--possibilities for translating the DATETIME to DATE
declare @TestDate1 date = @SomeDate;
declare @TestDate2 date = cast(@SomeDate as date);

select
@TestDate1 as TestDate1,
@TestDate2 as TestDate2;


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>