Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

nvarchar to numeric conversion error issue with derived tables

$
0
0

I have an issue with a varchar column while using derived tables but it works fine with temp tables.

createtable#A(idint,grp_idint);

insertinto#A values(1,1),(2,1),(3,2),(4,2)

createtable#B(idint,areavarchar(100),revenueint);

insertinto#B values(1,'100',5),(2,'200',10),(3,'n/a',10)

Now the below query raises an error.

Conversion failed when converting the varchar value 'n/a' to data type int.

 SELECTid,gid,sum(area)         

 FROM             

 (      

 SELECTa.idasid,a.grp_idasgid,b.area/b.revenueasarea      

 FROM#B b            

 JOIN#A a              

 ON     b.id=a.id             

 AND     a.grp_id=1     

 )D             

 GROUPBY id,gid

The same query runs fine when i use temp tables instead of temporary tables.

   SELECTa.idasid,a.grp_idasgid,b.area/b.revenueasarea into#tmp     

 FROM#B b            

 JOIN#A a              

 ON     b.id=a.id             

 AND     a.grp_id=1

  SELECTid,gid,sum(area)         

 FROM #tmp

 GROUPBY id,gid

Please let me know whats the issue with derived tables here . How can I solve it inside derived tables with out using temp tables?

Thanks.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>