这可能很简单,但我不是SQL高手,所以我迷路了。我理解sql接受你的查询并按特定顺序执行它,我相信这个查询不起作用的原因:
select * from purchaseorders
where IsNumeric(purchase_order_number) = 1
and cast(purchase_order_number as int) >= 7
所有的buyer_order_number字段都是数字,但我们最近引入了字母数字字段。我想要获得的数据是查看“7”是否大于最高数字purchase_order_number。
Numeric()函数可以很好地过滤掉字母数字字段,但进行后续的强制转换比较会引发此错误:
Conversion failed when converting the nvarchar value '124-4356AB' to data type int.
我不是在问这个错误意味着什么,这很明显。我问是否有办法在单个查询中完成我想要的东西,最好是因为ORM约束而在where子句中。
答案 0 :(得分:27)
这对你有用吗?
select * from purchaseorders
where (case when IsNumeric(purchase_order_number) = 1
then cast(purchase_order_number as int)
else 0 end) >= 7
答案 1 :(得分:1)
您可以使用子选择
进行选择select * from (
select * from purchaseorders
where IsNumeric(purchase_order_number) = 1) as correct_orders
where cast(purchase_order_number as int) >= 7
答案 2 :(得分:0)
试试这个:
select * from purchaseorders
where try_cast(purchase_order_number as int) >= 7