使用SQL Server,假设您将十进制存储为nvarchar,并且您希望使用“like”将其与另一个nvarchar进行比较。假设您提供有效数字,这应该没问题(尽管不理想):
declare @test1 nvarchar(18);
declare @test2 nvarchar(18);
set @test1 = '1.15%';
set @test2 = '1.1500000000000000';
select
case when @test1 like @test2 then 'Yes'
else 'No'
end as result;
这会返回“否”的结果,为什么会这样?
编辑:在回答答案时,有没有一天?哈,谢谢你的帮助。答案 0 :(得分:3)
LIKE
的右侧操作数不是字符串文字,而是可以包含%
和_
作为通配符的模式。在您的情况下,右侧操作数为@test2
,不包含此类通配符,因此仅匹配与其相同的字符串。
如果您反转订单,即执行@test2 LIKE @test1
,您将看到预期的结果。
答案 1 :(得分:3)
因为@test1
语句中的@test2
和LIKE
是错误的...
select
case when @test2 like @test1 then 'Yes'
else 'No'
end as result;