SQL Server 2008 R2 issue with a similar sentence

I want to search on any column of the table that the user selects. For this I have two string variables to search for the column name and searchvalue for the value to be searched for.

This is my request for the same:

and (Select Case @SearchKey 
            When 'Title' then Parcel.Title 
            When 'Unit_No' then Unit_No 
            When 'AgentName' then App_User.Name
            When 'TenantName' then Client_Personal_Information.First_Name
            When 'UnitRefNo' then Unit_Ref_No

                        End )  like @SearchValue

      

this query works fine and gives me the desired result since all columns are nvarchar.

But when I added this in the previous request

When 'Rent' then Unit_Transform.Rent_Per_Annum
When 'SecurityDeposit' then [Unit_Transform].[Security_Deposit] 

      

for columns that contain decimal values, it only returns the output for those two columns. If I select the column that stores the nvarchar values, it doesn't find the record it finds.

Any ideas why this is happening.

+3


source to share


1 answer


I think you need to CAST your decimal fields to varchar like this:

When 'Rent' then CAST( Unit_Transform.Rent_Per_Annum as Varchar )

      



And here's a Fiddle to see. Remove the cast and you actually get the error.

Good luck.

+1


source







All Articles