SQLTeam.com | Weblogs | Forums

Error in coverting nvarchar to int

sql2008r2

#1

Hi,
I want to find the max value of nvarchar type .
I tried following example but returns error
create table #temp1(rctno int,total nvarchar)
insert into #temp1 values(100,500)
insert into #temp1 values(101,500)
insert into #temp1 values(102,500)
select SUM(cast(total as int)) as totalval,MAX(rctno) as maval from #temp1
drop table #temp1

following is the error
Msg 8115, Level 16, State 2, Line 2

Arithmetic overflow error converting expression to data type nvarchar.

The statement has been terminated.

Msg 8115, Level 16, State 2, Line 3

Arithmetic overflow error converting expression to data type nvarchar.

The statement has been terminated.

Msg 8115, Level 16, State 2, Line 4

Arithmetic overflow error converting expression to data type nvarchar.

The statement has been terminated.

(1 row(s) affected)
how to solve this
Regards
Baiju


#2

You need to specify a length on the nvarchar column:

create table #temp1(rctno int,total nvarchar (30))


#3

By-the-way thank you for the table definition / consumable data / and the error message. That helps a lot when we try and help you.