Solved

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

Posted on 2011-02-21
5
805 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now