[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-02-21
5
Medium Priority
?
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

649 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