Reputation: 23
I have a database on SQL Server 2012 (with 2008 compatibility level), and my code looks like this:
DECLARE @BeginTime time
...
SELECT CONVERT(datetime2, BeginDate + ' ' + @BeginTime)
FROM MyTable ...
BeginDate
is defined as smalldatetime
.
This code works well, until I switch the database to 2012 compatibility level, in which case this error shows up:
SqlException : smalldatetime and time types are incompatible with add operator...
Should I convert my 2 operands to varchar
before proceeding? Worried about the performance ...
Thanks for your help.
Upvotes: 1
Views: 978
Reputation: 69524
Try something like this...
DECLARE @BeginTime time;
SET @BeginTime = '12:20:00.467'
SELECT CONVERT(datetime2, BeginDate + CONVERT(SmallDateTime, @BeginTime))
FROM MyTable
Upvotes: 1