Performing a LIKE comparison on an INT field

前端 未结 2 1508
感动是毒
感动是毒 2020-12-09 15:49

I\'m fairly new to SQL and I was trying to get a full list of products that match a user input of a productID. Something like:

SELECT ProductID, ProductName          


        
2条回答
  •  醉梦人生
    2020-12-09 16:39

    You can convert int to string using CONVERT(ProductID, CHAR(16)) AS ProductID and then use LIKE. So in your case it would be

    SELECT ProductID, ProductName FROM Products
    WHERE CONVERT(ProductID, CHAR(16)) LIKE '%15%'
    

    You should remember that the query will make a full table scan without any support from indices. As a result it will be really expensive and time consuming.

提交回复
热议问题