Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

14 Experts available now in Live!

Get 1:1 Help Now