Solved

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

Posted on 2009-03-06
5
1,082 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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 …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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