Thomas Titanski
Thomas Titanski

Reputation: 21

cast date to varchar then cast it back to date

I was working on a T-SQL query that someone else wrote and seen this.

AND Cast(m.MeasurementDateTime as date) >= cast('''+cast(@StartDate as varchar(100)) + ''' as date)
AND cast(m.MeasurementDateTime as date) <= cast('''+cast(@EndDate   as varchar(100)) + ''' as date)'

Why would someone do this? Am I missing something? Its the same thing as

CAST(@StartDate AS DATE)

right? The @EndDate and @StartDate are coming from a report so I don't the source data type.

Upvotes: 2

Views: 612

Answers (1)

Giorgos Altanis
Giorgos Altanis

Reputation: 2760

Can you print @StartDate to see its format? If it has an integer format for example we cannot cast it directly to a date:

select cast(20031016 as date)
Explicit conversion from data type int to date is not allowed.

select cast(cast(20031016 as varchar) as date)
16.10.2003 00:00:00

Upvotes: 3

Related Questions