Barth33
Barth33

Reputation: 23

SqlException : smalldatetime and time types are incompatible with add operator

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

Answers (1)

M.Ali
M.Ali

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

Related Questions