I recently created in a SQL Server 2008 dev environment a function that concatenates a date and time like this
select cast('2016-11-09 15:35:00' AS DATE) + CAST('00:00:00' AS DATETIME)
In SQL Server 2008 it works well but deployed in SQL Server 2016 it throws an error
The data types date and datetime are incompatible in the add operator.
But it works well ONLY if queries are placed separately
select cast('2016-11-09 15:35:00' AS DATE)
select CAST('00:00:00' AS DATETIME)
So, how can I fix this. I found articles where people say that there is an incompatibility with TIME
data type, but I am not using it. In fact, my concatenation is like this
WHERE
CREATIONDATE BETWEEN CAST(@CurrentDate AS DATE) + CAST('00:00:00' AS DATETIME) AND CAST(@CurrentDate AS DATE) + CAST('23:59:59' AS DATETIME)
where @CurrentDate
is a DateTime
variable and passed to my function as '2016-11-09 15:35:00'
I cannot modify the configuration of the SQL Server 2016 box. How can I fix my code?
Thanks
Just cast it as a date and then a datetime.
select cast(cast('2016-11-09 15:35:00' as date) as datetime)
To get 1 more day, use:
dateadd select dateadd(dd,1,cast(cast('2016-11-09 15:35:00' as date) as datetime))
I'm not a huge fan of the between operator so when i deal with datetimes i tend to use > startDate and < endDate where the endDate is the next day , so 11/10 which is like saying <= 11/09 23:59:59
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With