2

My query gets data from two tables and join them to give output as a single output. Subquery works without any error. But when i run the whole query i get an error like this.

Arithmetic overflow error converting varchar to data type numeric.

This is my query

SELECT a.REF_NO + ' - '
       + (SELECT DISTINCT ( c.c_name )
          FROM   C_MASTER c,
                 A_MASTER a
          WHERE  a.c_no = c._NO) AS refer
FROM   [A_MASTER] a,
       [C_MASTER] c
WHERE  a.c_no = c._no
ORDER  BY a.REF_NO   

REF_NO is numeric data type.

2
  • Show example data and desired results. Commented Sep 12, 2016 at 7:19
  • result should be like this => 10001 - annual competition Commented Sep 12, 2016 at 7:36

2 Answers 2

3

Try this:

select CAST(a.REF_NO AS VARCHAR(100))
        +' - '
        +(select distinct (c.c_name) from C_MASTER c, A_MASTER a
            where a.c_no=c._NO) as refer
from [A_MASTER] a, [C_MASTER] c
where a.c_no=c._no
order by a.REF_NO

Overall this looks like a sloppy query to me, but I can't improve upon it without more explanation about what you're trying to do, what you want your results to look like, and what your tables look like. Also, is there a reason you aren't using joins?

2
  • @Stam when i do so i get an exception like this "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'REF_NO'." Commented Sep 12, 2016 at 7:34
  • 1
    @user6592730 You need to add REF_NO as an alias to the column. You would have gotten the same error if your original query worked - there is no REF_NO column. Commented Sep 12, 2016 at 7:39
1

Try out with the below query. Use a join instead of separating tables with comma when writing queries.

 Select  distinct  cast(a.ref_no as VARCHAR(50))+'-'+c.c_name 
 from C_MASTER c
      Join A_MASTER a
                        On a.c_no=c._NO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.