我有一个包含以下值的表
ACCNO TEXT
------------- ----------------
8611004050001 internal payment
861100405000X external payment
8611XXXXXXXXX other payments
假设如果accono是8611004050002那么我必须拍照外部付款。 如果accno是8611211216223,那么我必须得到其他付款。
我需要获得最接近的匹配值是单个sql我尝试过case语句它不起作用。实现这一目标的有效方法是什么。
请帮助我
答案 0 :(得分:0)
嗯,一种方法使用巨大的case
语句。像这样:
select t.*
from t
order by (case when accno = <accno> then 99
when left(accno, 15) = left(<accno>, 15) then 15
when left(accno, 14) = left(<accno>, 14) then 14
when left(accno, 13) = left(<accno>, 13) then 13
when left(accno, 12) = left(<accno>, 12) then 12
when left(accno, 11) = left(<accno>, 11) then 11
when left(accno, 10) = left(<accno>, 10) then 10
when left(accno, 9) = left(<accno>, 9) then 9
when left(accno, 8) = left(<accno>, 8) then 8
when left(accno, 7) = left(<accno>, 7) then 7
when left(accno, 6) = left(<accno>, 6) then 6
when left(accno, 5) = left(<accno>, 5) then 5
when left(accno, 4) = left(<accno>, 4) then 4
when left(accno, 3) = left(<accno>, 3) then 3
when left(accno, 2) = left(<accno>, 2) then 2
when left(accno, 1) = left(<accno>, 1) then 1
else 0
end) desc
fetch first 1 row only;