Solved

Conversion failed when converting the varchar value 'null' to data type int.

Posted on 2011-02-21
5
813 Views
Last Modified: 2012-05-11
Hello Experts,

I have the following SQL quaery, that is giving me:

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'null' to data type int.

However, if I remove the second-last line it works fine.

Reffering to the second-last line, [tblVouchers].fldVouchID is defined as primary key int auto increment, so it can never be null.

When I run this query in SQL server management studio it "executes with errors" reffering to the above.

Any ideas?
SELECT [vouch].fldDealName, [purch].fldVouchNumber,
 ([inv].fldFName + ' ' + [inv].fldLName) AS fldPurchBy,
 [inv].fldTransDate, [inv].fldTransTime,
 [purch].fldRedeemComment, [purch].fldRedeemed, [purch].fldRedeemDateTime 
FROM tblUsers AS users, 
 tblVouchers AS vouch, 
 tblPurchases AS purch, 
 tblInvoices AS inv
WHERE [purch].fldInvID = [inv].fldInvID 
AND [inv].fldRespCode < 50
AND [purch].fldVouchRecID = [vouch].fldVouchID 
AND [vouch].fldBizID = [users].fldUserID 
AND [users].fldUserID = 18 
AND [vouch].fldVouchID = 11
ORDER BY [inv].fldTransDate DESC, [inv].fldTransTime DESC

Open in new window

0
Comment
Question by:APD_Toronto
  • 3
  • 2
5 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 34948148
Try this one:
SELECT [vouch].fldDealName, [purch].fldVouchNumber,
 ([inv].fldFName + ' ' + [inv].fldLName) AS fldPurchBy,
 [inv].fldTransDate, [inv].fldTransTime,
 [purch].fldRedeemComment, [purch].fldRedeemed, [purch].fldRedeemDateTime 
FROM tblUsers AS users, 
 tblVouchers AS vouch, 
 tblPurchases AS purch, 
 tblInvoices AS inv
WHERE [purch].fldInvID = [inv].fldInvID 
AND [inv].fldRespCode < 50
AND [purch].fldVouchRecID = CAST(ISNULL([vouch].fldVouchID, '0') AS INT)
AND [vouch].fldBizID = [users].fldUserID 
AND [users].fldUserID = 18 
AND CAST(ISNULL([vouch].fldVouchID, '0') AS INT) = 11
ORDER BY [inv].fldTransDate DESC, [inv].fldTransTime DESC

Open in new window

0
 

Author Comment

by:APD_Toronto
ID: 34948156
OK, worked.

Could you please explain to me what it means

AND CAST(ISNULL([vouch].fldVouchID, '0') AS INT) = 11
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34948167
>> Conversion failed when converting the varchar value 'null' to data type int.

I have exactly worked to fix the above error.
Since you pass int value 11 as input to fldVouchID column, it tries to implicitly convert that column to int and in case of any null values it was erroring.
So, I first converted Null to '0' and then converted it to int explicitly so that the conversion will never fail..

Or you can also try the simpler version like

AND [vouch].fldVouchID = 11
0
 

Author Closing Comment

by:APD_Toronto
ID: 34948203
Thank You
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34948227
Welcome..
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question