Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
583 views
in SQL Queries by 17 20 26

I got an error "Conversation failed when varchar to data type int".

When I execute the below query

Declare @year int, @month int, @age  nvarchar(50)
Select @year = Datediff(year,'1985-12-30 11:09:33.590',GETDATE()) 
Set @age = @year+' years old'
Select @age

 


1 Answer

0 like 0 dislike
by 17 25 39

This error occurs because you are trying to add INT data type to Nvarchar data type and this is not possible!
So, you can use the convert function or cast function to solve this error Conversation failed when varchar to data type int as below:

Replace this statement

Set @age = @year +' years old'

with

Set @age = cast(@year as nvarchar)+' years old'

See also CAST and CONVERT

If you don’t ask, the answer is always NO!
...