Jump to content

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

- - - - -

  • Please log in to reply
2 replies to this topic

#1
TheWebGuy

TheWebGuy

    Newbie

  • Members
  • Pip
  • 7 posts
This is driving me crazy

I run this query in the sql management studio and it works fine. but seems like it doesn't work in the stored procedure

Can I not use a LIKE '%' in an integer field?

here is my stored procedure


USE [ReefJunkies]

GO

/****** Object:  StoredProcedure [dbo].[SearchLibraryFish]    Script Date: 11/06/2010 21:22:50 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[SearchLibraryFish]


@CommonName Varchar(max),

@ReefCompatibilityID int,

@ReefCareLevelID int,

@ReefFamilyID int,

@FishTypeID int,

@ReefSpeciesID int,

@ReefDietID int,

@ReefTemperamentID int



AS


SET @CommonName= '%' + @CommonName + '%'

SET @ReefCompatibilityID= @ReefCompatibilityID + '%'

SET @ReefCareLevelID= @ReefCareLevelID + '%'

SET @ReefFamilyID= @ReefFamilyID + '%'

SET @FishTypeID= @FishTypeID + '%'

SET @ReefSpeciesID= @ReefSpeciesID + '%'

SET @ReefDietID= @ReefDietID + '%'

SET @ReefTemperamentID= @ReefTemperamentID + '%'


SELECT *

FROM  FishLibrary INNER JOIN

               FishType ON FishLibrary.FishTypeID = FishType.FishTypeID INNER JOIN

               ReefCompatibility ON FishLibrary.ReefCompatibilityID = ReefCompatibility.ReefCompatibilityID INNER JOIN

               ReefCareLevel ON FishLibrary.ReefCareLevelID = ReefCareLevel.ReefCareLevelID INNER JOIN

               ReefFamily ON FishLibrary.ReefFamilyID = ReefFamily.ReefFamilyID INNER JOIN

               ReefSpecies ON FishLibrary.ReefSpeciesID = ReefSpecies.ReefSpeciesID INNER JOIN

               ReefDiet ON FishLibrary.ReefDietID = ReefDiet.ReefDietID INNER JOIN

               ReefTemperament ON FishLibrary.ReefTemperamentID = ReefTemperament.ReefTemperamentID

WHERE (FishLibrary.CommonName LIKE @CommonName) AND (FishLibrary.ReefCompatibilityID LIKE @ReefCompatibilityID) AND 

               (FishLibrary.ReefCareLevelID LIKE @ReefCareLevelID) AND (FishLibrary.ReefFamilyID LIKE @ReefFamilyID) AND (FishLibrary.FishTypeID LIKE @FishTypeID) AND

                (FishLibrary.ReefSpeciesID LIKE @ReefSpeciesID) AND (FishLibrary.ReefDietID LIKE @ReefDietID) AND 

               (FishLibrary.ReefTemperamentID LIKE @ReefTemperamentID)





#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
I don't understand why doing an LIKE '%', as that means "always true", and then there is no meaning with it. And, why use it otherwise, say it had worked, if I had an LIKE '1%', it would match on 1, 10, 11, 12, and 12535 as well, it just makes no sence in LIKeing an integer. just use the ordinary operators, =, <, >, <=, >=, <> instead...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Like is used with text fields, not numeric fields. You would have to cast your integer to a string before you can use LIKE with it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users