像整数运算符
我的表中有一个 bigint (ProductSerial) 类型的列.我需要使用 like 运算符按产品序列过滤表.但我发现,像运算符不能用于整数类型.
I have a column of type bigint (ProductSerial) in my table. I need to filter the table by the Product serial using like operator. But I found that, like operator can't be used for integer type.
有没有其他方法可以做到这一点(我不想使用 =
运算符).
Is there any other method for this (I don't want to use the =
operator).
如果你必须使用LIKE
,你可以将你的号码转换为char
/varchar
,并对结果执行 LIKE
.这是非常低效的,但由于 LIKE
无论如何都有很大的可能杀死索引,因此它可能适用于您的场景:
If you must use LIKE
, you can cast your number to char
/varchar
, and perform the LIKE
on the result. This is quite inefficient, but since LIKE
has a high potential of killing indexes anyway, it may work in your scenario:
... AND CAST(phone AS VARCHAR(9)) LIKE '%0203'
如果您希望使用 LIKE
来匹配数字的开头或结尾,您可以使用整数除法和取模运算符来提取数字.例如,如果您想要所有以 407
开头的九位数字,请搜索
If you are looking to use LIKE
to match the beginning or the end of the number, you could use integer division and modulus operators to extract the digits. For example, if you want all nine-digit numbers starting in 407
, search for
phone / 1000000 = 407