Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query below , in that with out count function .i got answer,if using count function ,i got this error ---->Conversion failed when converting the varchar value '')' to data type int.

Please crack me out..
SQL
select 'INSERT INTO CM_ONTHEFLYCOMMUNICATION (c_source,c_classification,c_name,c_email,c_mobile,c_bookscount) 
      values ( '''+'Library'+''','''+isnull('CheckOut -'+cio_branchname,'')+''',
      '''+ISNULL(cio_membername,'')+''','''+ISNULL(mstmember.m_email1,'')+''',
      '''+ISNULL(mstmember.m_mobile,'')+''','''+count(trncheckinout.cio_bookname)+''')' 

     AS MAILQRY
     from 
    trncheckinout inner join
    mstmember on cio_mrecid=m_recid where CIO_CheckOutDt = 20141208
    group by m_email1,m_mobile, cio_branchname,cio_membername
Posted
Updated 8-Dec-14 21:59pm
v2
Comments
syed shanu 9-Dec-14 2:15am    
try this if this not works paste your insert and select query create and insert script so that other can find for the solution.

insert into CM_ONTHEFLYCOMMUNICATION (c_source,c_classification,c_name,c_email,c_mobile,c_bookscount)
values
Select 'Library'
,'CheckOut -'+cio_branchname
,cio_membername
,mstmember.m_email1
,mstmember.m_mobile
,count(trncheckinout.cio_bookname)
from
trncheckinout inner join
mstmember on cio_mrecid=m_recid where CIO_CheckOutDt = 20141208
group by m_email1,m_mobile, cio_branchname,cio_membername
Bns Vigneshwaran 9-Dec-14 2:23am    
I want o/p like this sir, please see Below .. In that at last you see 'THE SURVIVORS CLUB' <-- its a book, i want count of that .. so only i structured query like that,so i used count function there..Thank you

INSERT INTO CM_ONTHEFLYCOMMUNICATION (c_source,c_classification,c_name,c_email,c_mobile,c_bookscount)
values ( 'Library','CheckOut -seevaram ',
'D253,RAMYA DEVI BRAMYA DEVI B','rami.rogfed@gmail.com',
'+919445354610','THE SURVIVORS CLUB')
You should use Parameterized query.

The problem is string comcatenation is using the same operator as addition: +. If one of the operands is a number SQL server assumes it's addition and tries to convert the rest to numbers too. So what you need is to convert result of count to varchar:
SQL
select 'INSERT INTO CM_ONTHEFLYCOMMUNICATION (c_source,c_classification,c_name,c_email,c_mobile,c_bookscount) 
      values ( ''Library'','''+isnull('CheckOut -'+cio_branchname,'')+''',
      '''+ISNULL(cio_membername,'')+''','''+ISNULL(mstmember.m_email1,'')+''',
      '''+ISNULL(mstmember.m_mobile,'')+''','''+cast(count(trncheckinout.cio_bookname) as varchar(100))+''')' 
 
     AS MAILQRY
     from 
    trncheckinout inner join
    mstmember on cio_mrecid=m_recid where CIO_CheckOutDt = 20141208
    group by m_email1,m_mobile, cio_branchname,cio_membername

I took the liberty to remove the concatenation around the first operand 'Library'.
 
Share this answer
 
Comments
Bns Vigneshwaran 9-Dec-14 4:44am    
U genius :) Thank for ur support and to Code Project WEBSITE :)
exec the below query.



SQL
insert into CM_ONTHEFLYCOMMUNICATION (c_source,c_classification,c_name,c_email,c_mobile,c_bookscount)
Select 'Library'
,'CheckOut -'+cio_branchname
,cio_membername
,mstmember.m_email1
,mstmember.m_mobile
,count(trncheckinout.cio_bookname)
from
trncheckinout inner join
mstmember on cio_mrecid=m_recid where CIO_CheckOutDt = 20141208
group by m_email1,m_mobile, cio_branchname,cio_membername
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900