Solved

Conversion failed when converting the varchar value '-1"' to data type int.

Posted on 2009-03-06
5
1,088 Views
Last Modified: 2012-06-21
My CF developer couldn't pass me NULLS so we decided on -1 (a value) that would never present itself in our db.  I tested the sp with the -1 values and everything worked well but when she called the sp, received the following error:

Conversion failed when converting the varchar value '-1"' to data type int.

A portion of my code is below...

Thx

ALTER PROCEDURE [dbo].[usp_Search2]

@Program varchar (50) = -1,
@Project varchar (255) = NULL,
@Center varchar (50) = NULL,
@Facility_Name varchar (255)= NULL,
@BuildNos varchar (255) = NULL,
SELECT
f.MainID,
p.Program,
p.Project,
p.Center,
f.Facility_Name,
f.Building_Nos,
From...
WHERE
(ISNULL(@Program,-1) = -1 or p.Program = @Program)    
and (ISNULL (@Project, -1)= -1 or p.Project = @Project)
and (ISNULL (@Center,-1) = -1 or p.Center = @Center)
and (ISNULL (@Facility_Name,-1)= -1 or f.Facility_Name = @Facility_Name)
and (ISNULL (@BuildNos, -1)= -1  or f.Building_Nos = @BuildNos)
0
Comment
Question by:Glen_D
[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
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Lotok
ID: 23815132
Why do you need it set as a varchar?
Could you not use:


@Program tinyint = -1,

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 23815142
Check whther all have datatype varchar

p.Program,
p.Project,
p.Center,
f.Facility_Name,
f.Building_Nos,

any one of this field amy have datatype int
0
 

Author Comment

by:Glen_D
ID: 23815148
The input from the front screen (for the parameters) comes from lookup tables in varchar format; my default value was NULL but CF has an issue with NULLS so we decied to use the -1.  I checked all my fields and everything is set to varchar; SQL is trying to convert the -1 to an int.
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 23815185
ALTER PROCEDURE [dbo].[usp_Search2]

@Program varchar (50) = '-1',
@Project varchar (255) = NULL,
@Center varchar (50) = NULL,
@Facility_Name varchar (255)= NULL,
@BuildNos varchar (255) = NULL,
SELECT
f.MainID,
p.Program,
p.Project,
p.Center,
f.Facility_Name,
f.Building_Nos,
From...
WHERE
(ISNULL(@Program,'-1') = '-1' or p.Program = @Program)    
and (ISNULL (@Project, '-1')= '-1' or p.Project = @Project)
and (ISNULL (@Center,'-1') = '-1' or p.Center = @Center)
and (ISNULL (@Facility_Name,'-1')= '-1' or f.Facility_Name = @Facility_Name)
and (ISNULL (@BuildNos, '-1')= '-1'  or f.Building_Nos = @BuildNos)
 
Tags:
MS SQL 2005/2008
0
 

Author Comment

by:Glen_D
ID: 23815226
wow...that looks so simple; by quoting the -1 values, I'll prevent SQL from automatically trying to convert?

Thx
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

Suggested Solutions

Title # Comments Views Activity
Trying to understand why my Index is so large 12 52
grouping by date only 6 22
SQL 2008 - Disappearing Temp Table in Stored Procedure 24 63
SQL query 45 41
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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