SQL Type Conversion
Posted on 2013-01-06
I have modified the code to find text in tables to what you see below.
SET @sql_string = 'IF EXISTS (SELECT * FROM ' + @table_name + ' WHERE ' + @column_name + ' LIKE ''%' + @search_string + '%'') PRINT ''select * from ' + @table_name + ' where ' + @column_name + ' like ''%' + @search_string + '%''' + ''''
I am getting this error message on the second occurence of @search_string.
Conversion failed when converting the varchar value 'select * from IV30500 where DOCNUMBR like ' to data type int.
I believe what is happening is that often the value can be all numeric so SQL converts it to a numeric type field as opposed to just treating it as all text. How do I adjust this portion to simply treat whatever is read as text? Do I need to put a case statement that looks for all digits and then converts to string while leaving any values that do have characters alone? If so, what would the syntax look like?