我的表格如下所示
SalesOrderNo
------------
1
2
D1
D2
D11
D12
D16
D20
D21
我想要搜索并找到比D16更重要的所有销售工程师 这就是我试过的
Select SalesOrderNo From tbl_Sales Where SalesOrderNo > 'D16' and SalesOrderNo LIKE 'D%'
这就是我得到的
D2
D20
D21
The Desierd Out Put是
D20
D21
答案 0 :(得分:3)
使用RIGHT
函数在右侧部分找到整数。
<强>查询强>
select SalesOrderNo from tbl_sales
where SalesOrderNo LIKE 'D%'
and cast(right(SalesOrderNo, len(SalesOrderNo) - 1) as int) > 16;
答案 1 :(得分:0)
试试这个。
假设在D之后只有数字。
Select SalesOrderNo From tbl_Sales
Where to_number(replace(SalesOrderNo,'D','')) > 16
and SalesOrderNo LIKE 'D%'
答案 2 :(得分:0)
您需要一种方法来表示D和任何其他带有数字的字母。
select * from
@tbl_Sales
where
order by
case
when SalesOrderNo like 'D%' then cast(replace(SalesOrderNo,'D','') as int) + 100000
else cast(SalesOrderNo as int)
END > 100016--D16
order by
case
when SalesOrderNo like 'D%' then cast(replace(SalesOrderNo,'D','') as int) + 100000
else cast(SalesOrderNo as int)
END
虽然如果你有一个大表,我建议你创建另一个列并用这个整数值填充它以达到性能目的。
答案 3 :(得分:0)
您可以使用以下查询
DECLARE @InputColumn AS VARCHAR(20)
SET @InputColumn = 'D16'
SELECT SalesOrder
FROM dbo.Section
WHERE CONVERT(INT, SUBSTRING(SalesOrder, PATINDEX('%[0-9]%', SalesOrder),
LEN(SalesOrder))) > CONVERT(INT, SUBSTRING(@InputColumn,
PATINDEX('%[0-9]%',
@InputColumn),
LEN(@InputColumn)))
ORDER BY LEFT(SalesOrder, PATINDEX('%[0-9]%', SalesOrder) - 1) ,
CONVERT(INT, SUBSTRING(SalesOrder, PATINDEX('%[0-9]%', SalesOrder),
LEN(SalesOrder)))