Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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